英文:
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 {
// ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论