1. go-micro体验 1package main 2 3import ( 4 "fmt" 5 "github.com/micro/go-micro/web" 6 "net/http" 7) 8 9func main(){ 10 service := web.NewService(web.Address("127.0.0.1:8000")) 11 // 使用http原生的handlefunc 12 service.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 13 w.Write([]byte("hello world")) 14 }) 15 err := service.Run() 16 if err != nil { 17 fmt.Println(err) 18 } 19} 测试结果 1curl http://127.0.0.1:8000 2hello world 2. 使用web框架gin 1package main 2 3import ( 4 "github.com/gin-gonic/gin" 5 "github.com/micro/go-micro/web" 6 "net/http" 7) 8 9func main() { 10 r := gin.Default() 11 r.Handle("GET", "/", func(c *gin.Context) { 12 c.JSON(http.StatusOK, gin.H{ 13 "code": http.StatusOK, 14 }) 15 }) 16 service := web.NewService( 17 web.Name("demo_service"), 18……
阅读全文