go-micro.dev/v5@v5.12.0/internal/website/docs/examples/hello-service.md (about)

     1  ---
     2  layout: default
     3  ---
     4  
     5  # Hello Service
     6  
     7  A minimal HTTP service using Go Micro, with a single endpoint.
     8  
     9  ## Service
    10  
    11  ```go
    12  package main
    13  
    14  import (
    15      "context"
    16      "go-micro.dev/v5"
    17  )
    18  
    19  type Request struct { Name string `json:"name"` }
    20  
    21  type Response struct { Message string `json:"message"` }
    22  
    23  type Say struct{}
    24  
    25  func (h *Say) Hello(ctx context.Context, req *Request, rsp *Response) error {
    26      rsp.Message = "Hello " + req.Name
    27      return nil
    28  }
    29  
    30  func main() {
    31      svc := micro.New("helloworld")
    32      svc.Init()
    33      svc.Handle(new(Say))
    34      svc.Run()
    35  }
    36  ```
    37  
    38  Run it:
    39  
    40  ```bash
    41  go run main.go
    42  ```
    43  
    44  Call it:
    45  
    46  ```bash
    47  curl -XPOST \
    48    -H 'Content-Type: application/json' \
    49    -H 'Micro-Endpoint: Say.Hello' \
    50    -d '{"name": "Alice"}' \
    51    http://127.0.0.1:8080
    52  ```
    53  
    54  Set a fixed address:
    55  
    56  ```go
    57  svc := micro.NewService(
    58      micro.Name("helloworld"),
    59      micro.Address(":8080"),
    60  )
    61  ```