Hello World

Hello World

Overview

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

Sample

  • etc/helloworld.yaml
  • main.go
Name: HelloWorld.api
Host: 127.0.0.1
Port: 8080
``` ```
func main() {
    var restConf rest.RestConf
    conf.MustLoad("etc/helloworld.yaml", &restConf)
    s, err := rest.NewServer(restConf)
    if err != nil {
        log.Fatal(err)
        return
    }

    s.AddRoute(rest.Route{ // Add routes
        Method: http.MethodGet,
        Path:   "/hello/world",
        Handler: func(writer http.ResponseWriter, request *http.Request) { // HTTP Handler
            httpx.OkJson(writer, "Hello World!")
        },
    })

    defer s.Stop()
    s.Start() // Run service
}

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