web
package main
import (
"net/http"
"github.com/micro/go-micro/v2/util/log"
"github.com/micro/go-micro/v2/web"
)
func main() {
server := web.NewService(web.Address(":8080"))
server.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong\n"))
})
log.Fatal(server.Run())
}
go mod
require (
github.com/micro/go-micro/v2 v2.9.1
)
启动服务器
go run main.go
测试
$curl localhost:8080/ping
pong
gin
package main
import (
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/util/log"
"github.com/micro/go-micro/v2/web"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.Writer.Write([]byte("pong\n"))
})
server := web.NewService(web.Address(":8080"))
server.Handle("/ping", r)
log.Fatal(server.Run())
}
go.mod文件
require (
github.com/gin-gonic/gin v1.6.3
github.com/micro/go-micro/v2 v2.9.1
)