How to make http.Get request inside of handler func? For example, this simple code "should" return blank page in localhost:8080 browser but it go nuts. What I have missed in school?
package main
import "net/http"
func index(w http.ResponseWriter, r *http.Request) {
_, err := http.Get("www.google.com")
if err != nil {
panic(err)
}
}
func main() {
http.HandleFunc("/", index)
http.ListenAndServe(":8080", nil)
}
The problem is that you should use a protocol (e.g. https://
) in the Get
function:
_, err := http.Get("https://www.google.com")
The error in you original code is Get www.google.com: unsupported protocol scheme ""
.
http.Get()
expects a URL, and www.google.com
is not a URL; a URL begins with a scheme. Even though it is common to type "www.google.com" into a browser, it's still not a full URL; friendly browsers automatically prepend "http://" or "https://" before issuing the request. http.Get()
isn't going to do that; it expects a well-formed URL to begin with.