Start a Go HTTP server by defining a handler function and calling http.ListenAndServe with your desired port.
Use the net/http package to define a handler function and pass it to http.ListenAndServe to start the server.
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
})
http.ListenAndServe(":8080", nil)
}
Creating an HTTP server in Go involves writing code that listens for incoming requests on port 8080. When a user visits the homepage, the server responds with a simple text message. It is the standard way to build web services in Go without needing external frameworks.