user2443447 user2443447 - 7 days ago 5
HTTP Question

newbie compilation error net/http Response

Why do I get a compilation error for this code? Response type is defined in "net/http"

package main
import "net/http"
func main() {
}
func GetWithProxy(urlString string, proxyString string) (resp *Response, err error) {
return nil, nil
}


error:

.\t.go:3: imported and not used: "net/http"
.\t.go:7: undefined: Response

Answer

It's complaining that you didn't use net/http, which you didn't.

package main

import "net/http"

func GetWithProxy(urlString string, proxyString string) (resp *http.Response, err error) {
        return nil, nil
}

func main() {
}

This will compile because now you are using net/http. The compiler didn't know you were talking about net/http's Response type.

If you want 'absorb' net/http's namespace you can do:

package main
import . "net/http"

func GetWithProxy(urlString string, proxyString string) (resp *Response, err error) {
    return nil, nil
}

func main() {
}

Observe: https://play.golang.org/p/WH1NSzFhSV