github.com/moleculer-go/moleculer@v0.3.3/examples/standalone/math.service.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/moleculer-go/moleculer"
     7  	"github.com/moleculer-go/moleculer/broker"
     8  	"github.com/moleculer-go/moleculer/payload"
     9  )
    10  
    11  var mathService = moleculer.ServiceSchema{
    12  	Name: "math",
    13  	Actions: []moleculer.Action{
    14  		{
    15  			Name: "add",
    16  			Handler: func(ctx moleculer.Context, params moleculer.Payload) interface{} {
    17  				return params.Get("a").Int() + params.Get("b").Int()
    18  			},
    19  		},
    20  	},
    21  }
    22  
    23  func main() {
    24  	var bkr = broker.New(&moleculer.Config{LogLevel: "error"})
    25  	bkr.Publish(mathService)
    26  	bkr.Start()
    27  	result := <-bkr.Call("math.add", payload.New(map[string]int{
    28  		"a": 10,
    29  		"b": 130,
    30  	}))
    31  	fmt.Println("result: ", result.Int()) //$ result: 140
    32  	bkr.Stop()
    33  }