go - resp declared and not used in golang -


I get this error every time I try to run the code I'm unsure how to use unused variables How to code and work without unused variables To post this question, I had to remove one another's statement. The problem is as follows: If (case1) calls a URL and if (case2) calls another URL, then calls to other URL.

Thanks for the help. I tried various hacks but none of them are working.

  Package main import ("fmt" "math / rand" "net / http") func handler (w http.ResponseWriter, r * Http.Request) {anger: = rand.intnt ( )% 3 var resp * response if anger == 1 {resp, _: = http.get ("https://maps.googleapis.com/maps/api/place/ Textsearch / json? Query = Restaurants + in + Sydney and amp; key = Aaisa SAH 4 T-Kyuelmaiaraksanke 0 XF0Yiu9ZXVFDNfPTCFs ")) and {resp, _: = http.Get (" https://maps.googleapis.com/maps/api/place/nearbysearch / Json? location = -33.8670522 , 151.1957362 & amp; radius = 500 & amp; types = food & amp; name = cruise & amp; key = AIzaSyAh4t-qlMYrxnk0XF0Yiu9ZXVFDNfPTCFs ")} postponed resp.Body.Close () body, _: = ioutil.ReadAll (resp. body) resp = unused.Item Fmt.Println ("Inside Handler") fmt.Fprintf (w, c Tring (body), null)} func main () {http.HandleFunc ( "/", handler) HttpkListenAndServe ( "localhost: 99 99", null)} output: C: \ go \ gowiki \ websrevice_citibytes_SOAP.go: 233: then C declared and not used C: \ go \ gowiki \ websrevice_citibytes_SOAP.go: 235: resp declared and not use C: \ go \ gowiki \ websrevice_citibytes_SOAP.go: 237: resp declared and not used   

There are some errors in your code:

var resp * Feedback var res * http.Response

should be import "IO / ioutil" if you read its ReadAll Docs> Function.

What is resp = unused.Item ? It should be removed.

After you fix all those things, as the other comments say: Remove: in those rows that re-provide it:

resp, _: = http.Get ("...") -> resp, _ = http.Get ("...")

The error is that creating a new variable resp using : = , which overrides the previous one and only if the scope remains inside.

As other suggestions have been given, it would be better to use string variables with the URL and set that variable.

  var url string = url1 if cond {url = url2} resp, Err: = http.get (url)  

In addition, Do not ignore errors


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

How to access user directory in lazarus? -

java - Gradle dependencies: compile project by relative path -