Here's an example code snippet that demonstrates how to return a simple HTML response from an HTTP server in Go:
main.go242 chars16 lines
In this example, we define a handler
function that takes an http.ResponseWriter
and an http.Request
. The http.ResponseWriter
is used to send the HTTP response, and the http.Request
contains information about the incoming request.
Inside the handler
function, we use the fmt.Fprintf
function to write a simple HTML string to the http.ResponseWriter
. This string will be sent back to the client as the HTTP response body.
Finally, we use the http.HandleFunc
function to associate the handler
function with the root URL path ("/"). We then start the HTTP server by calling http.ListenAndServe
, passing nil
as the second argument, which uses the default HTTP handler.
By running this program and visiting http://localhost:8080 in your browser, you should see "Hello, world!" displayed in a large heading.
gistlibby LogSnag