golang http server manage post code example
Example 1: Golang HTTP Server
package main
import (
"fmt"
"log"
"net/http"
)
const (
Host = "localhost"
Port = "8080"
)
func home(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is a Simple HTTP Web Server!")
}
func main() {
http.HandleFunc("/", home)
err := http.ListenAndServe(Host+":"+Port, nil)
if err != nil {
log.Fatal("Error Starting the HTTP Server : ", err)
return
}
}
Example 2: 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)
}