【golang】22、functional options | 函数式编程、闭包

文章目录

  • 一、配置 Option
    • 1.1 options
    • 1.2 funcitonal options

一、配置 Option

1.1 options

https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html

I’ve been trying on and off to find a nice way to deal with setting options in a Go package I am writing. Options on a type, that is. The package is intricate and there will probably end up being dozens of options. There are many ways to do this kind of thing, but I wanted one that felt nice to use, didn’t require too much API (or at least not too much for the user to absorb), and could grow as needed without bloat.

I’ve tried most of the obvious ways: option structs, lots of methods, variant constructors, and more, and found them all unsatisfactory. After a bunch of trial versions over the past year or so, and a lot of conversations with other Gophers making suggestions, I’ve finally found one I like. You might like it too. Or you might not, but either way it does show an interesting use of self-referential functions.

I hope I have your attention now.

Let’s start with a simple version. We’ll refine it to get to the final version.

First, we define an option type. It is a function that takes one argument, the Foo we are operating on.

type option func(*Foo)

The idea is that an option is implemented as a function we call to set the state of that option. That may seem odd, but there’s a method in the madness.

Given the option type, we next define an Option method on *Foo that applies the options it’s passed by calling them as functions. That method is defined in the same package, say pkg, in which Foo is defined.

This is Go, so we can make the method variadic and set lots of options in a given call:

// Option sets the options specified.
func (f *Foo) Option(opts ...option) {for _, opt := range opts {opt(f)}
}

Now to provide an option, we define in pkg a function with the appropriate name and signature. Let’s say we want to control verbosity by setting an integer value stored in a field of a Foo. We provide the verbosity option by writing a function with the obvious name and have it return an option, which means a closure; inside that closure we set the field:

// Verbosity sets Foo's verbosity level to v.
func Verbosity(v int) option {return func(f *Foo) {f.verbosity = v}
}

Why return a closure instead of just doing the setting? Because we don’t want the user to have to write the closure and we want the Option method to be nice to use. (Plus there’s more to come…)

In the client of the package, we can set this option on a Foo object by writing:

foo.Option(pkg.Verbosity(3))

That’s easy and probably good enough for most purposes, but for the package I’m writing, I want to be able to use the option mechanism to set temporary values, which means it would be nice if the Option method could return the previous state. That’s easy: just save it in an empty interface value that is returned by the Option method and the underlying function type. That value flows through the code:

type option func(*Foo) interface{}// Verbosity sets Foo's verbosity level to v.
func Verbosity(v int) option {return func(f *Foo) interface{} {previous := f.verbosityf.verbosity = vreturn previous}
}// Option sets the options specified.
// It returns the previous value of the last argument.
func (f *Foo) Option(opts ...option) (previous interface{}) {for _, opt := range opts {previous = opt(f)}return previous
}

The client can use this the same as before, but if the client also wants to restore a previous value, all that’s needed is to save the return value from the first call, and then restore it.

prevVerbosity := foo.Option(pkg.Verbosity(3))
foo.DoSomeDebugging()
foo.Option(pkg.Verbosity(prevVerbosity.(int)))

The type assertion in the restoring call to Option is clumsy. We can do better if we push a little harder on our design.

First, redefine an option to be a function that sets a value and returns another option to restore the previous value.

type option func(f *Foo) option


This self-referential function definition is reminiscent of a state machine. Here we’re using it a little differently: it’s a function that returns its inverse.

Then change the return type (and meaning) of the Option method of *Foo to option from interface{}:

// Option sets the options specified.
// It returns an option to restore the last arg's previous value.
func (f *Foo) Option(opts ...option) (previous option) {for _, opt := range opts {previous = opt(f)}return previous
}

The final piece is the implementation of the actual option functions. Their inner closure must now return an option, not an interface value, and that means it must return a closure to undo itself. But that’s easy: it can just recur to prepare the closure to undo the original! It looks like this:

// Verbosity sets Foo's verbosity level to v.
func Verbosity(v int) option {return func(f *Foo) option {previous := f.verbosityf.verbosity = vreturn Verbosity(previous)}
}

Note the last line of the inner closure changed from
return previous
to
return Verbosity(previous)
Instead of just returning the old value, it now calls the surrounding function (Verbosity) to create the undo closure, and returns that closure.

Now from the client’s view this is all very nice:

prevVerbosity := foo.Option(pkg.Verbosity(3))
foo.DoSomeDebugging()
foo.Option(prevVerbosity)

And finally we take it up one more level, using Go’s defer mechanism to tidy it all up in the client:

func DoSomethingVerbosely(foo *Foo, verbosity int) {// Could combine the next two lines,// with some loss of readability.prev := foo.Option(pkg.Verbosity(verbosity))defer foo.Option(prev)// ... do some stuff with foo under high verbosity.
}

It’s worth noting that since the “verbosity” returned is now a closure, not a verbosity value, the actual previous value is hidden. If you want that value you need a little more magic, but there’s enough magic for now.

The implementation of all this may seem like overkill but it’s actually just a few lines for each option, and has great generality. Most important, it’s really nice to use from the point of view of the package’s client. I’m finally happy with the design. I’m also happy at the way this uses Go’s closures to achieve its goals with grace.

1.2 funcitonal options

https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/657263.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

人机认知何时、何处趋同?何时、何处趋异?

对于“算法与人类认知的差异”,人机认知是否应该趋同还是趋异,这是一个复杂的问题,没有简单的答案。 一方面,人机认知的趋同可以使人类能够更好地理解和利用算法的能力,从而提高工作效率和生活质量。趋同意味着人类可以…

数学公式OCR识别php 对接mathpix api 使用公式编译器

数学公式OCR识别php 对接mathpix api 一、注册账号官网网址:https://mathpix.com 二、该产品支持多端使用注意说明(每月10次) 三、api 对接第一步创建create keyphp对接api这里先封装两个请求函数,get 和post ,通过官方…

20240130在ubuntu20.04.6下卸载NVIDIA显卡的驱动

20240130在ubuntu20.04.6下卸载NVIDIA显卡的驱动 2024/1/30 12:58 缘起,为了在ubuntu20.4.6下使用whisper,以前用的是GTX1080M,装了535的驱动。 现在在PDD拼多多上了入手了一张二手的GTX1080,需要将安装最新的545的驱动程序&#…

VxTerm:SSH工具中的中文显示和乱码时的相关信息和一些基本的知识

当我们写的程序含有控制台(Console)输出时,如果输入内容包含中文时,我们一般需要知道下面的信息,才能正确的搞清楚怎么处理中文显示的问题: 1、实际程序或文件中的实际编码: Linux下的应用程序和文本文件,…

CVE-2024-0352 likeshop v2.5.7文件上传漏洞分析

本次的漏洞研究基于thinkPHP开发开的一款项目..... 漏洞描述 Likeshop是Likeshop开源的一个社交商务策略的完整解决方案,开源免费版基于thinkPHP开发。Likeshop 2.5.7.20210311及之前版本存在代码问题漏洞,该漏洞源于文件server/application/api/contr…

云原生Kubernetes: Ubuntu 安装 K8S 1.23版本(单Master架构) 及故障恢复

目录 一、实验 1.环境 2.安装 Ubuntu 3.连接Ubuntu 4.master节点安装docker 5.node节点安装docker 6.master节点安装K8S 7.添加K8S工作节点 8.安装网络插件calico 9.故障 10.故障恢复 11.测试k8s网络和coredns 二、问题 1.Ubuntu如何修改镜像源 2.Ubuntu和Windo…

C++开发基础之生产者和消费者模型:实现多线程数据交换与同步

0.前言 在并发编程中,处理多个线程之间的数据交换和同步是一个常见而重要的挑战。生产者和消费者模型是一种经典的解决方案,它为我们提供了一种简单而灵活的方法来协调多个线程的操作。无论是构建消息队列、日志记录系统还是任务调度系统,生…

Java 数组形参

java 基本数据类型传递参数时是值传递 ;引用类型传递参数时是引用传递 。 我们根据三道问题来探索一下是什么意思。 首先来看一个问题。 问题一 以下方法调用传递给程序是什么 ? double[] rats {1.2, 3.4, 5.6}; routine( rats );A、rats 的副本 …

DataTable.Load(reader)注意事项

对于在C#中操作数据库查询,这样的代码很常见: using var cmd ExecuteCommand(sql); using var reader cmd.ExecuteReader(); DataTable dt new DataTable(); dt.Load(reader); ...一般的查询是没问题的,但是如果涉及主键列的查询&#xf…

protobuf简介(一)

1.protobuf简介 Protobuf是Protocol Buffers的简称,它是Google公司开发的一种数据描述语言,是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化 。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议…

保定兴芮科技有限公司环卫市场化运营服务项目招标公告

项目概况 保定兴芮科技有限公司环卫市场化运营服务项目招标项目的潜在投标人应在河北省公共资源交易服务平台获取招标文件,并于2024年02月20日09点00分(北京时间)前递交投标文件。 一、项目基本情况 项目编号:QDZB2024-010 项目名称:保定兴芮…

洛谷P1540 机器翻译

参考代码 #include<iostream> #include<queue> using namespace std; int nums[1010]; int main(){queue<int> q;int M,N;cin>>M>>N;int res0;while(N--){int tmp;cin>>tmp;if(nums[tmp]1)continue;if(q.size()<M){q.push(tmp);res;nu…

交叉注意力融合时域、频域特征的FFT + CNN-Transformer-CrossAttention轴承故障识别模型

目录 往期精彩内容&#xff1a; 前言 1 快速傅里叶变换FFT原理介绍 第一步&#xff0c;导入部分数据 第二步&#xff0c;故障信号可视化 第三步&#xff0c;故障信号经过FFT可视化 2 轴承故障数据的预处理 2.1 导入数据 2.2 制作数据集和对应标签 3 交叉注意力机制 …

网站地址怎么改成HTTPS?

现在&#xff0c;所有类型的网站都需要通过 HTTPS 协议进行安全连接&#xff0c;而实现这一目标的唯一方法是使用 SSL 证书。如果您不将 HTTP 转换为 HTTPS&#xff0c;浏览器和应用程序会将您网站的连接标记为不安全。 但用户询问如何将我的网站从 HTTP 更改为 HTTPS。在此页…

移动端设计规范 - 文字使用规范

这是一篇关于移动端产品界面设计时&#xff0c;文字大小的使用规范&#xff0c;前端人员如果能了解一点的话&#xff0c;在实际开发中和设计沟通时&#xff0c;节省沟通成本&#xff0c;也能提高设计落地开发时的还原度。 关于 在做移动端产品设计时&#xff0c;有时候使用文字…

【开源精选导航】GitHub-Chinese-Top-Charts:一榜在手,优质中文项目轻松找寻

各位热爱开源技术的朋友们&#xff0c;你们是否有过这样的困扰&#xff1a;面对浩瀚的GitHub海洋&#xff0c;想找寻那些具有高质量中文文档的优秀开源项目却无从下手&#xff1f;今天&#xff0c;我们就为大家揭晓一个宝藏般的开源项目——GitHub 中文项目集合&#xff08;访问…

二维数组移动,合并数值简易2048

2848简易核心运算 --多元素合并数组举例4*4 -- 星空露珠韩永旗制作 --数据合并并重新赋值 --多元素合并数组举例4*4 -- 星空露珠韩永旗制作 --数据合并并重新赋值 local data{{0,2,0,2}, {4,2,0,8}, {8,0,8,4}, {2,2,4,8}} local ch{{1…

【Node.js基础】Node.js的介绍与安装

文章目录 前言一、什么是Node.js&#xff1f;二、安装Node.js2.1 Windows系统2.2 macOS系统2.3 Linux系统 三、运行js代码总结 前言 随着互联网技术的不断发展&#xff0c;构建高性能、实时应用的需求日益增长。Node.js作为一种服务器端运行时环境&#xff0c;以其事件驱动、非…

万户 ezOFFICE SendFileCheckTemplateEdit.jsp SQL注入漏洞

0x01 产品简介 万户OA ezoffice是万户网络协同办公产品多年来一直将主要精力致力于中高端市场的一款OA协同办公软件产品,统一的基础管理平台,实现用户数据统一管理、权限统一分配、身份统一认证。统一规划门户网站群和协同办公平台,将外网信息维护、客户服务、互动交流和日…

uniapp-app使用富文本编辑器editor

使用的是uniapp内置组件的表单组件editor&#xff1a;editor 组件 | uni-app官网 (dcloud.net.cn) 文档上写的也不是特别详细&#xff0c;还以为得npm&#xff0c;但没npm也能用 注意&#xff1a;editor不能封装为组件&#xff0c;否则报错&#xff08;在其他文章看的&#x…