11

Golang的可选参数实践

 3 years ago
source link: https://jiajunhuang.com/articles/2020_04_20-golang_optional_parameters.md.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Golang的可选参数实践

Golang处理可选参数+默认参数的时候,常见的代码是这样的:

type Queue struct {
	Name string
}

func NewQueue(name string) *Queue {
	return &Queue{name}
}

然而随着项目的发展,这个参数可能会变多:

type Queue struct {
	Name     string
	MaxLimit int
}

那么这个时候要如何处理呢?有几种方案:

  • 破坏兼容性:使用 func NewQueue(name string, maxLimit int) *Queue 的方式
  • 不破坏兼容性,增加 func NewQueueWithLimit(name string, maxLimit int) *Queue,从而保留原来的方式
  • 破坏兼容性,使用一个 Config 来保存配置,就变成了 func NewQueue(config *QueueConfig) *Queue,但是这种方式有一个 很大的缺点,就是不好处理零值,零值到底是零值,还是填的刚好是零值?我该不该用默认参数替代之?

因此就演变出下面这种方式,在我看来是一个很好的解决方案:

type Queue struct {
	Name     string
	MaxLimit int

	// monitor
	MonitorInterval int
}

type QueueOption func(*Queue)

func WithMaxLimit(max int) QueueOption {
	return func(q *Queue) {
		q.MaxLimit = max
	}
}

func WithMonitorInterval(seconds int) QueueOption {
	return func(q *Queue) {
		q.MonitorInterval = seconds
	}
}

func NewQueue(name string, options ...QueueOption) *Queue {
	queue := &Queue{name, 10, 5}

	for _, o := range options {
		o(queue)
	}

	return queue
}

使用起来,就可以这样:NewQueue("abcd", WithMaxLimit(10)),然而后面的 With 函数是可选的,如果不选,就会使用我们的默认值, 这样做的好处是不会破坏兼容性,以后如果增加了更多选项,那么增加一个 With函数即可。

不过天下没有免费的午餐,这种方式的弊端也很明显,就是比较费键盘 — 每次新增一个选项时都要新增加一个函数。


参考资料:


微信公众号
关注公众号,获得及时更新

自己写个搜索引擎

HTTP 路由的两种常见设计形式

Golang的short variable declaration

Greenlet和Stackless Python

写一个简单的ORM

从源码看Python的descriptor

Python字符串格式化

Gunicorn 简明教程

Raft 论文阅读笔记

什么是 Golang Comparable Types

GFS 论文阅读

MapReduce 论文阅读

一起来做贼:Goroutine原理和Work stealing

Go语言的defer, panic和recover

再读vim help:vim小技巧




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK