To create a REST API in Go, we can use the standard built-in package "net/http" and use "gorilla/mux" package for routing. Here is a simple example to create a REST API with HTTP methods GET, POST, PUT and DELETE:
main.go2100 chars73 lines
This example defines the Person and Address structs, creates four endpoints, and populates the people slice with some initial data. We can then use the http.ListenAndServe
function to start the server and listen to incoming requests on port 8080.
The above code can be run in the command prompt using "go run" command or can be compiled into a binary file using "go build" command, and then that binary file can be run.
Note: Don't forget to install the "gorilla/mux" package before running or compiling the code. Use "go get github.com/gorilla/mux" command to install.
gistlibby LogSnag