This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Learn by Example

A collection of small, focused examples demonstrating common patterns with Go Micro.

1 - Hello Service

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

Service

package main

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

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.NewService("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("helloworld",
    micro.Address(":8080"),
)

2 - Rpc Client

Call a running service using the Go Micro client.
package main

import (
    "context"
    "fmt"
    "go-micro.dev/v6"
)

type Request struct { Name string }

type Response struct { Message string }

func main() {
    svc := micro.NewService("caller")
    svc.Init()

    req := svc.Client().NewRequest("helloworld", "Say.Hello", &Request{Name: "John"})
    var rsp Response

    if err := svc.Client().Call(context.TODO(), req, &rsp); err != nil {
        fmt.Println("error:", err)
        return
    }

    fmt.Println(rsp.Message)
}

3 - Service Discovery with Consul

Use Consul as the service registry.

In code

package main

import (
    "go-micro.dev/v6"
    "go-micro.dev/v6/registry/consul"
)

func main() {
    reg := consul.NewConsulRegistry()
    svc := micro.NewService("consul-registry", micro.Registry(reg))
    svc.Init()
    svc.Run()
}

Via environment

Run your service with env vars set:

MICRO_REGISTRY=consul MICRO_REGISTRY_ADDRESS=127.0.0.1:8500 go run main.go

4 - Store Postgres

Use the Postgres store for persistent key/value state.

In code

package main

import (
    "log"
    "go-micro.dev/v6"
    "go-micro.dev/v6/store"
    postgres "go-micro.dev/v6/store/postgres"
)

func main() {
    st := postgres.NewStore()
    svc := micro.NewService("postgres-store", micro.Store(st))
    svc.Init()

    _ = store.Write(&store.Record{Key: "foo", Value: []byte("bar")})
    recs, _ := store.Read("foo")
    log.Println("value:", string(recs[0].Value))

    svc.Run()
}

Via environment

Run your service with env vars set:

MICRO_STORE=postgres \
MICRO_STORE_ADDRESS=postgres://user:pass@127.0.0.1:5432/postgres \
MICRO_STORE_DATABASE=micro \
MICRO_STORE_TABLE=micro \
go run main.go

5 - Transport Nats

Use NATS as the transport between services.

In code

package main

import (
    "go-micro.dev/v6"
    tnats "go-micro.dev/v6/transport/nats"
)

func main() {
    t := tnats.NewTransport()
    svc := micro.NewService("nats-transport", micro.Transport(t))
    svc.Init()
    svc.Run()
}

Via environment

Run your service with env vars set:

MICRO_TRANSPORT=nats MICRO_TRANSPORT_ADDRESS=nats://127.0.0.1:4222 go run main.go