To create a HTTP server with a PUT route in Go, you can use the built-in net/http
package. Here's an example code:
main.go430 chars25 lines
In this code, we define a putHandler
function that checks if the HTTP method is PUT, and returns a message accordingly. We then register this handler function with the /put
route in our main
function using http.HandleFunc
.
Finally, we start the server by calling http.ListenAndServe(":8080", nil)
and passing in nil
as the handler since we've already registered it with http.HandleFunc
.
Now, if you make a PUT request to http://localhost:8080/put
, you should see the message "This is a PUT request" in your browser or API client.
gistlibby LogSnag