前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go - 学习 grpc.Dial(target string, opts …DialOption) 的写法

Go - 学习 grpc.Dial(target string, opts …DialOption) 的写法

作者头像
新亮
发布2020-10-10 16:52:16
1.8K0
发布2020-10-10 16:52:16
举报
文章被收录于专栏:新亮笔记新亮笔记

咱们平时是这样使用 grpc.Dial 方法的,比如:

代码语言:javascript
复制
conn, err := grpc.Dial("127.0.0.1:8000",
  grpc.WithChainStreamInterceptor(),
  grpc.WithInsecure(),
  grpc.WithBlock(),
  grpc.WithDisableRetry(),
 )

咱们怎么能写出类似这样的调用方式,它是怎么实现的?

这篇文章咱们写一个 Demo,其实很简单,一步步往下看。

opts …DialOption,这个是不定参数传递,参数的类型为 DialOption,不定参数是指函数传入的参数个数为不定数量,可以不传,也可以为多个。

写一个不定参数传递的方法也很简单,看看下面这个方法 1 + 2 + 3 = 6。

代码语言:javascript
复制
func Add(a int, args ...int) (result int) {
 result += a
 for _, arg := range args {
  result += arg
 }
 return
}

fmt.Println(Add(1, 2, 3))

// 输出 6

其实平时我们用的 fmt.Println()fmt.Sprintf() 都属于不定参数的传递。

WithInsecure()WithBlock() 类似于这样的 With 方法,其实作用就是修改 dialOptions 结构体的配置,之所以这样写我个人认为是面向对象的思想,当配置项调整的时候调用方无需修改。

场景

咱们模拟一个场景,使用 不定参数WithXXX 这样的写法,写个 Demo,比如我们要做一个从附近找朋友的功能,配置项有:性别、年龄、身高、体重、爱好,我们要找性别为女性,年龄为30岁,身高为160cm,体重为55kg,爱好为爬山的人,希望是这样的调用方式:

代码语言:javascript
复制
friends, err := friend.Find("附近的人",
  friend.WithSex(1),
  friend.WithAge(30),
  friend.WithHeight(160),
  friend.WithWeight(55),
  friend.WithHobby("爬山"))

代码实现

代码语言:javascript
复制
// option.go

package friend

import (
 "sync"
)

var (
 cache = &sync.Pool{
  New: func() interface{} {
   return &option{sex: 0}
  },
 }
)

type Option func(*option)

type option struct {
 sex    int
 age    int
 height int
 weight int
 hobby  string
}

func (o *option) reset() {
 o.sex = 0
 o.age = 0
 o.height = 0
 o.weight = 0
 o.hobby = ""
}

func getOption() *option {
 return cache.Get().(*option)
}

func releaseOption(opt *option) {
 opt.reset()
 cache.Put(opt)
}

// WithSex setup sex, 1=female 2=male
func WithSex(sex int) Option {
 return func(opt *option) {
  opt.sex = sex
 }
}

// WithAge setup age
func WithAge(age int) Option {
 return func(opt *option) {
  opt.age = age
 }
}

// WithHeight set up height
func WithHeight(height int) Option {
 return func(opt *option) {
  opt.height = height
 }
}

// WithWeight set up weight
func WithWeight(weight int) Option {
 return func(opt *option) {
  opt.weight = weight
 }
}

// WithHobby set up Hobby
func WithHobby(hobby string) Option {
 return func(opt *option) {
  opt.hobby = hobby
 }
}

代码语言:javascript
复制
// friend.go

package friend

import (
 "fmt"
)

func Find(where string, options ...Option) (string, error) {
 friend := fmt.Sprintf("从 %s 找朋友\n", where)

 opt := getOption()
 defer func() {
  releaseOption(opt)
 }()

 for _, f := range options {
  f(opt)
 }

 if opt.sex == 1 {
  sex := "性别:女性"
  friend += fmt.Sprintf("%s\n", sex)
 }
 if opt.sex == 2 {
  sex := "性别:男性"
  friend += fmt.Sprintf("%s\n", sex)
 }

 if opt.age != 0 {
  age := fmt.Sprintf("年龄:%d岁", opt.age)
  friend += fmt.Sprintf("%s\n", age)
 }

 if opt.height != 0 {
  height := fmt.Sprintf("身高:%dcm", opt.height)
  friend += fmt.Sprintf("%s\n", height)
 }

 if opt.weight != 0 {
  weight := fmt.Sprintf("体重:%dkg", opt.weight)
  friend += fmt.Sprintf("%s\n", weight)
 }

 if opt.hobby != "" {
  hobby := fmt.Sprintf("爱好:%s", opt.hobby)
  friend += fmt.Sprintf("%s\n", hobby)
 }

 return friend, nil
}

代码语言:javascript
复制
// main.go

package main

import (
 "demo/friend"
 "fmt"
)

func main() {
 friends, err := friend.Find("附近的人",
  friend.WithSex(1),
  friend.WithAge(30),
  friend.WithHeight(160),
  friend.WithWeight(55),
  friend.WithHobby("爬山"))

 if err != nil {
  fmt.Println(err)
 }

 fmt.Println(friends)
}

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-10-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 新亮笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 场景
  • 代码实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档