package main import ( "fmt" "log" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello world!") }) // log.Fatal shows you if there is an error like the // port already being used log.Fatal(http.ListenAndServe(":8080", nil)) }
Save this file as Main.go
in a directory called GoHTTPServer
, run go build && ./GoHTTPServer
. Then, open http://localhost:8080 in your browser and see the result for yourself.