编写单例结构的更短方法
如果我想创建一个“单例”结构,我可以执行以下操作:
foo := struct{
bar func(string, int, bool) error
}{ bar: func(a string, b int, c bool) error {
// ...
}}
如您所见,我必须将 bar
的签名写入两次。有没有更短的方法来写这个?
If I want to create a "singleton" struct, I can do the following:
foo := struct{
bar func(string, int, bool) error
}{ bar: func(a string, b int, c bool) error {
// ...
}}
as you can see I have to write bar
's signature twice. Is there a shorter way to write this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有更短的方法了。
如果该结构确实只有一个字段,您可能需要更改 foo 的类型:
There isn't a shorter way.
If the struct really has only one field, you may want to change
foo
's type: