1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-11-23 21:44:41 +02:00
Files
go-micro/internal/website/docs/examples/hello-service.md
2025-11-13 18:11:29 +00:00

875 B

layout
layout
default

Hello Service

A minimal HTTP service using Go Micro, with a single endpoint.

Service

package main

import (
    "context"
    "go-micro.dev/v5"
)

type Request struct { Name string `json:"name"` }

type Response struct { Message string `json:"message"` }

type Say struct{}

func (h *Say) Hello(ctx context.Context, req *Request, rsp *Response) error {
    rsp.Message = "Hello " + req.Name
    return nil
}

func main() {
    svc := micro.New("helloworld")
    svc.Init()
    svc.Handle(new(Say))
    svc.Run()
}

Run it:

go run main.go

Call it:

curl -XPOST \
  -H 'Content-Type: application/json' \
  -H 'Micro-Endpoint: Say.Hello' \
  -d '{"name": "Alice"}' \
  http://127.0.0.1:8080

Set a fixed address:

svc := micro.NewService(
    micro.Name("helloworld"),
    micro.Address(":8080"),
)