Go - 學習 grpc.Dial(target string, opts …DialOption) 的寫法

我們平時是這樣使用 grpc.Dial 方法的,好比:web

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。url

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() 都屬於不定參數的傳遞。spa

WithInsecure()WithBlock() 相似於這樣的 With 方法,其實做用就是修改 dialOptions 結構體的配置,之因此這樣寫我我的認爲是面向對象的思想,當配置項調整的時候調用方無需修改。.net

場景

我們模擬一個場景,使用 不定參數WithXXX 這樣的寫法,寫個 Demo,好比咱們要作一個從附近找朋友的功能,配置項有:性別、年齡、身高、體重、愛好,咱們要找性別爲女性,年齡爲30歲,身高爲160cm,體重爲55kg,愛好爲登山的人,但願是這樣的調用方式:code

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

代碼實現

// 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
 }
}

// 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
}

// 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)
}

輸出

從 附近的人 找朋友
性別:女性
年齡:30歲
身高:160cm
體重:55kg
愛好:登山



本文分享自微信公衆號 - 新亮筆記(XinLiangTalk)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。對象

相關文章
相關標籤/搜索