На фреймфорке gin, например, это будет выглядеть так:
package main
import (
"net/http"
"gopkg.in/gin-gonic/gin.v1"
)
func main() {
r := gin.Default()
r.GET("/ping", PingHandler)
r.GET("/user/:name", UserGetHandler)
r.POST("/user/:name", UserPostHandler)
r.Run()
}
func PingHandler(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
}
func UserGetHandler(c *gin.Context) {
name := c.Param("name")
c.String(http.StatusOK, "Hello %s\n", name)
}
func UserPostHandler(c *gin.Context) {
name := c.Param("name")
// Do some actions
c.String(http.StatusOK, "User %s was edited\n", name)
}
$ curl 127.0.0.1:8080/ping
{"message":"pong"}
$ curl 127.0.0.1:8080/user/john
Hello john
$ curl -XPOST 127.0.0.1:8080/user/john
User john was edited