Struct 编写单例结构的较短方法

Struct 编写单例结构的较短方法,struct,singleton,go,Struct,Singleton,Go,如果要创建“singleton”结构,可以执行以下操作: foo := struct{ bar func(string, int, bool) error }{ bar: func(a string, b int, c bool) error { // ... }} 如你所见,我必须写两次条形码的签名。写这篇文章有没有更短的方法?没有更短的方法 如果结构实际上只有一个字段,则可能需要更改foo的类型: foo := func(a string, b int, c bool)

如果要创建“singleton”结构,可以执行以下操作:

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

如你所见,我必须写两次
条形码的签名。写这篇文章有没有更短的方法?

没有更短的方法

如果结构实际上只有一个字段,则可能需要更改
foo
的类型:

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

没有比这更短的路了

如果结构实际上只有一个字段,则可能需要更改
foo
的类型:

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