Here's an example code snippet for creating an HTTP server on a specific port in Go:
main.go520 chars22 lines
In this code, we set the port
variable to the desired port number. We then use http.HandleFunc()
to set the request handler function. This function is called for each incoming request and is responsible for generating the response. In this example, we simply write "Hello, World!" to the response body.
Finally, we start the server by calling http.ListenAndServe()
with the port number and nil
as arguments. If there's an error, we log it with log.Fatal()
.
gistlibby LogSnag