github.com/moleculer-go/moleculer@v0.3.3/examples/math2/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  )
     9  
    10  type MathService struct {
    11  }
    12  
    13  func (s MathService) Name() string {
    14  	return "math"
    15  }
    16  
    17  func (s *MathService) Add(params moleculer.Payload) int {
    18  	return params.Get("a").Int() + params.Get("b").Int()
    19  }
    20  
    21  func (s *MathService) Sub(a int, b int) int {
    22  	return a - b
    23  }
    24  
    25  func main() {
    26  	var bkr = broker.New(&moleculer.Config{LogLevel: "error"})
    27  	bkr.Publish(&MathService{})
    28  	bkr.Start()
    29  	result := <-bkr.Call("math.add", map[string]int{
    30  		"a": 10,
    31  		"b": 130,
    32  	})
    33  	fmt.Println("result: ", result.Int())
    34  	//$ result: 140
    35  	bkr.Stop()
    36  }