更简洁的编写单例结构体的方法

huangapple go评论99阅读模式
英文:

Shorter way of writing singleton-structs

问题

如果我想创建一个“单例”结构体,我可以这样做:

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?

答案1

得分: 1

如果结构体确实只有一个字段,你可能想要改变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 <code>foo</code>'s type:

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

huangapple
  • 本文由 发表于 2012年2月17日 02:18:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/9316757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定