编写单例结构的更短方法

发布于 2025-01-06 11:59:56 字数 230 浏览 3 评论 0原文

如果我想创建一个“单例”结构,我可以执行以下操作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2025-01-13 11:59:56

没有更短的方法了。

如果该结构确实只有一个字段,您可能需要更改 foo 的类型:

foo := func(a string, b int, c bool) error {
    // ...
}

There isn't a shorter way.

If the struct really has only one field, you may want to change foo's type:

foo := func(a string, b int, c bool) error {
    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文