type CalculatorService struct{}func(s *CalculatorService)Add(a, b int)int{return a + b
}func(s *CalculatorService)Div(a, b int)(int,error){if b ==0{return0, errors.New("divide by zero")}return a / b,nil}
type CalculatorService struct{}func(s *CalculatorService)Add(ctx context.Context, a, b int)int{return a + b
}func(s *CalculatorService)Div(ctx context.Context, a, b int)(int,error){if b ==0{return0, errors.New("divide by zero")}return a / b,nil}
这样我们就可以获取到client,进而调用其方法,例如,我们在client端添加一个方法
type CalcService struct{}func(s *CalcService)GetParam(ctx context.Context)int{return88}
func (s *CalcService) Add(ctx context.Context, a, b int) int {client, ok := rpc.ClientFromContext(ctx)if ok {client.Call(&b, "calc_getParam")}return a + b
}