经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Go语言 » 查看文章
[系列] Go - 学习 grpc.Dial(target string, opts …DialOption) 的写法
来源:cnblogs  作者:新亮笔记  时间:2020/12/28 9:54:48  对本文有异议

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

  1. conn, err := grpc.Dial("127.0.0.1:8000",
  2. grpc.WithChainStreamInterceptor(),
  3. grpc.WithInsecure(),
  4. grpc.WithBlock(),
  5. grpc.WithDisableRetry(),
  6. )

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

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

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

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

  1. func Add(a int, args ...int) (result int) {
  2. result += a
  3. for _, arg := range args {
  4. result += arg
  5. }
  6. return
  7. }
  8. fmt.Println(Add(1, 2, 3))
  9. // 输出 6

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

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

场景

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

  1. friends, err := friend.Find("附近的人",
  2. friend.WithSex(1),
  3. friend.WithAge(30),
  4. friend.WithHeight(160),
  5. friend.WithWeight(55),
  6. friend.WithHobby("爬山"))

代码实现

  1. // option.go
  2. package friend
  3. import (
  4. "sync"
  5. )
  6. var (
  7. cache = &sync.Pool{
  8. New: func() interface{} {
  9. return &option{sex: 0}
  10. },
  11. }
  12. )
  13. type Option func(*option)
  14. type option struct {
  15. sex int
  16. age int
  17. height int
  18. weight int
  19. hobby string
  20. }
  21. func (o *option) reset() {
  22. o.sex = 0
  23. o.age = 0
  24. o.height = 0
  25. o.weight = 0
  26. o.hobby = ""
  27. }
  28. func getOption() *option {
  29. return cache.Get().(*option)
  30. }
  31. func releaseOption(opt *option) {
  32. opt.reset()
  33. cache.Put(opt)
  34. }
  35. // WithSex setup sex, 1=female 2=male
  36. func WithSex(sex int) Option {
  37. return func(opt *option) {
  38. opt.sex = sex
  39. }
  40. }
  41. // WithAge setup age
  42. func WithAge(age int) Option {
  43. return func(opt *option) {
  44. opt.age = age
  45. }
  46. }
  47. // WithHeight set up height
  48. func WithHeight(height int) Option {
  49. return func(opt *option) {
  50. opt.height = height
  51. }
  52. }
  53. // WithWeight set up weight
  54. func WithWeight(weight int) Option {
  55. return func(opt *option) {
  56. opt.weight = weight
  57. }
  58. }
  59. // WithHobby set up Hobby
  60. func WithHobby(hobby string) Option {
  61. return func(opt *option) {
  62. opt.hobby = hobby
  63. }
  64. }
  1. // friend.go
  2. package friend
  3. import (
  4. "fmt"
  5. )
  6. func Find(where string, options ...Option) (string, error) {
  7. friend := fmt.Sprintf("从 %s 找朋友\n", where)
  8. opt := getOption()
  9. defer func() {
  10. releaseOption(opt)
  11. }()
  12. for _, f := range options {
  13. f(opt)
  14. }
  15. if opt.sex == 1 {
  16. sex := "性别:女性"
  17. friend += fmt.Sprintf("%s\n", sex)
  18. }
  19. if opt.sex == 2 {
  20. sex := "性别:男性"
  21. friend += fmt.Sprintf("%s\n", sex)
  22. }
  23. if opt.age != 0 {
  24. age := fmt.Sprintf("年龄:%d岁", opt.age)
  25. friend += fmt.Sprintf("%s\n", age)
  26. }
  27. if opt.height != 0 {
  28. height := fmt.Sprintf("身高:%dcm", opt.height)
  29. friend += fmt.Sprintf("%s\n", height)
  30. }
  31. if opt.weight != 0 {
  32. weight := fmt.Sprintf("体重:%dkg", opt.weight)
  33. friend += fmt.Sprintf("%s\n", weight)
  34. }
  35. if opt.hobby != "" {
  36. hobby := fmt.Sprintf("爱好:%s", opt.hobby)
  37. friend += fmt.Sprintf("%s\n", hobby)
  38. }
  39. return friend, nil
  40. }
  1. // main.go
  2. package main
  3. import (
  4. "demo/friend"
  5. "fmt"
  6. )
  7. func main() {
  8. friends, err := friend.Find("附近的人",
  9. friend.WithSex(1),
  10. friend.WithAge(30),
  11. friend.WithHeight(160),
  12. friend.WithWeight(55),
  13. friend.WithHobby("爬山"))
  14. if err != nil {
  15. fmt.Println(err)
  16. }
  17. fmt.Println(friends)
  18. }

输出

  1. 附近的人 找朋友
  2. 性别:女性
  3. 年龄:30
  4. 身高:160cm
  5. 体重:55kg
  6. 爱好:爬山

原文链接:http://www.cnblogs.com/xinliangcoder/p/14197771.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号