handle get request and send response golang code example
Example 1: golang get request data
package main
func fetchResponse(url string) string{
resp, _ := http.Get(url)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return string(body)
}
func main() {
resp := fetchResponse("http://someurl.com")
fmt.Println(resp)
}
Example 2: golang http writer redirect
func redirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "http://www.google.com", 301)
}