Hello World

Overview

This section shows how to create a simple HTTP service using go-zero.

Sample

  • etc/helloworld.yaml
  • main.go
  1. Name: HelloWorld.api
  2. Host: 127.0.0.1
  3. Port: 8080
  1. func main() {
  2. var restConf rest.RestConf
  3. conf.MustLoad("etc/helloworld.yaml", &restConf)
  4. s, err := rest.NewServer(restConf)
  5. if err != nil {
  6. log.Fatal(err)
  7. return
  8. }
  9. s.AddRoute(rest.Route{ // Add routes
  10. Method: http.MethodGet,
  11. Path: "/hello/world",
  12. Handler: func(writer http.ResponseWriter, request *http.Request) { // HTTP Handler
  13. httpx.OkJson(writer, "Hello World!")
  14. },
  15. })
  16. defer s.Stop()
  17. s.Start() // Run service
  18. }

rest service configuration accessible HTTP service configuration

Can start a simple HTTP service in addition to using the above method

  1. Quickly create a HTTP service with goctl: goctl api new
  2. Quickly create and start a HTTP service with goctl, with reference goctl quickstart

References