Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The below Golang code has several errors. Please identify the errors and re - write the code to improve efficiencies and align better with Golang

The below Golang code has several errors. Please identify the errors and re-write the code to improve efficiencies and align better with Golang concepts. Submit your code submission in the answer below.
input.txt test input '{\"location\": \"foo\"}'{\"location\": \"foo.com\"}{\"location\": \"bar.com\"}{\"location\": \"httpbar.com\"}{\"location\": \"https://bar.com\"}---- package main import (\"bytes\"\"context\"\"encoding/json\"\"fmt\"\"io\"\"log\"\"net/http\"\"os\"\"strings\") var buf *bytes.Buffer func main(){ data, err := os.ReadFile(\"./input.txt\") if err != nil { log.Fatal(err)} lines := strings.Split(string(data),\"
\") for _, line := range lines { go func(){ data, err := getData(line) if err != nil { log.Printf(\"unable to get status: %v\", err)} if data.(string)==\"foo\"{ fmt.Printf(\"data found: %s\", data) os.Exit(0)}}()}} func getData(line string)(any, error){ var location struct { URL string } if err := json.Unmarshal([]byte(line), &location); err != nil {} req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, location.URL, nil) if err != nil { return nil, err } c := &http.Client{} res, err := c.Do(req) if err != nil { return nil, err } if _, err := io.Copy(buf, res.Body); err != nil { return nil, err } var payload struct { Data string `bson:\"data\"`} if err := json.Unmarshal(buf.Bytes(), &payload); err != nil { return nil, err } return payload.Data, nil }
*
Your answer

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2008 Comprehensive Concepts And Techniques

Authors: Gary B. Shelly, Corinne Hoisington

1st Edition

1423927168, 978-1423927167

More Books

Students also viewed these Databases questions

Question

Define cost as applied to the valuation of inventories.

Answered: 1 week ago

Question

Explain how cultural differences affect business communication.

Answered: 1 week ago

Question

List and explain the goals of business communication.

Answered: 1 week ago