咱們平時是這樣使用 grpc.Dial 方法的,比如:
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,
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,愛好為爬山的人,希望是這樣的呼叫方式:
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
愛好:爬山
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/241195.html
標籤:Go
