You can use the net/http
package to create a HTTP server in Go. Here's an example of how to return a JSON response with a specific status code:
main.go944 chars37 lines
In this example, we define a handler function that creates a Person
object, converts it to a JSON byte array using json.Marshal
, sets the Content-Type
header to application/json
, writes the JSON byte array to the response using w.Write
, and sets the status code using w.WriteHeader
. The http.Error
function is used to return a 500 Internal Server Error status code if there was an error converting the Person
object to JSON.
gistlibby LogSnag