GO 函数式选项模式(Functional Options Pattern)
作者:等你下课啊 发布时间:2024-05-22 10:16:05
标签:GO,函数式选项模式,Option模式
Golang 开发者遇到的许多问题之一是尝试将一个函数的参数设置为可选. 这是一个非常常见的用例, 有些对象应该使用一些基本的默认设置来开箱即用, 并且你偶尔可能需要提供一些更详细的配置.
在很多语言中这很容易; 在 C 族语言中, 可以使用不同数量的参数提供相同函数的多个版本; 在像 PHP 这样的语言中, 可以给参数一个默认值,并在调用方法时忽略它们. 但是在 Golang 中, 这两种方式你哪个也用不了. 那么你如何创建一个函数, 用户可以指定一些额外的配置?
有很多可能的方法可以做到这一点, 但是大多数都不能满足要求, 或者需要在服务端的代码中进行额外的检查和验证, 或者通过传递额外的客户端不关心的参数来为客户端做额外的工作.
下面介绍GO 函数式选项模式(Functional Options Pattern),内容如下:
Option模式的优缺点
优点:
1. 支持传递多个参数,并且在参数个数、类型发生变化时保持兼容性
2. 任意顺序传递参数
3. 支持默认值
4. 方便拓展
缺点:
1. 增加许多function,成本增大
2. 参数不太复杂时,尽量少用
DEMO
package main
import "fmt"
type Client struct {
Id int64
AppKey string
AppSecret string
}
type Option func(*Client) // go函数的参数都是值传递 因此想要修改Client(默认值) 必须传递指针
func WithAppKey(appKey string) Option {
return func(client *Client) {
client.AppKey = appKey
}
}
func WithAppSecret(appSecret string) Option {
return func(client *Client) {
client.AppSecret = appSecret
}
}
//
// NewClient
// @Description 将一个函数的参数设置为可选的功能
// @param id 固定参数,也可以将所有都放进可选参数 opts 中
// @param opts
// @return Client 返回 *Client 和 Client 都可以
//
func NewClient(id int64, opts ...Option) Client {
o := Client{
Id: id,
AppKey: "key_123456",
AppSecret: "secret_123456",
}
for _, opt := range opts {
opt(&o) // go函数的参数都是值传递 因此想要修改Client(默认值) 必须传递指针
}
return o
}
func main() {
// 使用默认值
fmt.Println(NewClient(1)) // {1 key_123456 secret_123456}
// 使用传入的值
fmt.Println(NewClient(2, WithAppKey("change_key_222"))) // {2 change_key_222 secret_123456}
// 不按照顺序传入
fmt.Println(NewClient(3, WithAppSecret("change_secret_333"))) // {3 key_123456 change_secret_333}
fmt.Println(NewClient(4, WithAppSecret("change_secret_444"), WithAppKey("change_key_444"))) // {4 change_key_444 change_secret_444}
}
来源:https://www.cnblogs.com/dibtp/p/15414148.html
0
投稿
猜你喜欢
- 前言最近在出差,发现住的宾馆居然有小强。所以出差无聊之际,写了点爬虫的代码玩玩,问就是应景。本篇文章主要是爬取CSDN全站综合热榜的100个
- 1、下载python3.6的安装包: wget https://www.python.org/ftp/p
- 行和列的位置都在以下三个列表中的一列中,则对应位置为1,其余位置全为0——[7-56,239-327,438-454,522-556,574
- 网上找了很多。貌似没什么好的解决办法。自己参考了下。把它直接整成一条SQL语句了。不过字段的格式只允许前面或者后面有字符。中间不允许出现字符
- Python 循环Python 有两个原始的循环命令:while 循环for 循环while 循环如果使用 while 循环,只要条件为真,
- 前言最新需要做一个小工具,让协作部门能够获取到服务器上的一些资源讯息,因为工具是pyqt写的所以牵扯到用python链接linux的问题,这
- MySQL提供标准的SQL模式匹配,以及一种基于象Unix实用程序如vi、grep和sed的扩展正则表达式模式匹配的格式。标准的SQL模式匹
- 如何使DIV居中,div垂直居中,div水平居中.这个问题在用CSS来设计网页的时候经常会遇到,如果用传统的表格来布局是很简单的,CSS里就
- 前言:如果大家接触过数据分析,那么大家可能都知道,最让人头疼的就是在数据录入的过程中,不可避免的会产生重复值,缺失值和异常值了,python
- mysql 8.0.11 winx64安装教程记录如下,分享给大家1.进入地址: 下载mysql-8.0.11-winx642.解
- 什么是接口测试接口测试主要用于检测外部系统与内部系统之间,以及系统内部各 个子系统之间的交互点。其测试的重点是,检查数据的交换、传递和控 制
- 我就把我学习Createjs的一些心得体会向大家分享下:一.什么是CreateJS?createjs是一个轻量级的javascript库,是
- 一、配置抓包工具1.安装软件本文选择的抓包工具:Fiddler 具体的下载安装这里不详细赘述!(网上搜Fiddler安
- 导 读vue3.0中,响应式数据部分弃用了 Object.defineProperty ,使用 Proxy 来代替它。本文将主要通过以下方面
- 创意404页面的文章我们似乎已经出过两篇了,今天hongkiat又带来了60个创意404页面.相关404页面设计文章:国外404错误页面的创
- 大一上学期学习的内容之一,小黑屋比较好玩。1.导入函数库先导入random、time两个函数库的使用来达到随机生成人物、生成人物加载时间的目
- 昨天群里介绍了一个专门帮你PS图片的网站。吐司网。网站在图片的预览处理上有点意思。当鼠标经过图片,显示为处理过的图片。这样大家能很清晰的对比
- 在Numpy中NaN值一般出现在数据清洗前,出现这个值说明这个数据是缺失的在有些时候我们会选择直接删除这些数据,但有些时候这些数据是不能删除
- 之前在Ubuntu 16.04安装 MySQL的时候很顺利,这次在 Ubuntu 18.04 中安装 MySQL 5.7.23 中,遇到一些
- 我就废话不多说了,直接上代码吧!第一种def test1(): l = [] for i in range(1000