英文:
Creating objects in Go
问题
我正在使用Go玩耍,这是一个例子。
type Foo struct {
Id int
}
func createFoo(id int) Foo {
return Foo{id}
}
对于小对象来说,这是完全可以的,但是如何为大对象创建工厂函数呢?
在这种情况下,最好返回指针以避免复制大量数据。
// 现在Foo有很多字段
func createFoo(id int /* 其他数据 */) *Foo {
x := doSomeCalc()
return &Foo{
Id: id,
// X: x和其他数据
}
}
或者
func createFoo(id int /* 其他数据 */) *Foo {
x := doSomeCalc()
f := new(Foo)
f.Id = id
// f.X = x和其他数据
return f
}
这两种方法有什么区别?哪种是标准的做法?
英文:
I'm playing around with Go for the first time. Consider this example.
type Foo struct {
Id int
}
func createFoo(id int) Foo {
return Foo{id}
}
This is perfectly fine for small objects, but how to create factory function for big objects?
In that case it's better to return pointer to avoid copying large chunks of data.
// now Foo has a lot of fields
func createFoo(id int /* other data here */) *Foo {
x := doSomeCalc()
return &Foo{
Id: id
//X: x and other data
}
}
or
func createFoo(id int /* other data here */) *Foo {
x := doSomeCalc()
f := new(Foo)
f.Id = id
//f.X = x and other data
return f
}
What's the difference between these two? What's the canonical way of doing it?
答案1
得分: 5
约定俗成的做法是编写NewFoo
函数来创建和初始化对象。例如:
如果你喜欢,你可以始终返回指针,因为在访问方法或属性时没有语法上的区别。我甚至可以说,返回指针通常更方便,这样你就可以直接在返回的对象上使用指针接收器方法。想象一个基本的结构体:
type Foo struct{}
func (f *Foo) M1() {}
当返回对象时,你无法这样做,因为返回的值是不可寻址的(playground上的示例):
NewFoo().M1()
当返回指针时,你可以这样做(playground上的示例)。
英文:
The convention is to write NewFoo
functions to create and initialize objects. Examples:
You can always return pointers if you like since there is no syntactic difference when accessing methods or attributes. I would even go as far and say that it is often more convenient to return pointers so that you can use pointer receiver methods directly on the returned object. Imagine a base like this:
type Foo struct{}
func (f *Foo) M1() {}
When returning the object you cannot do this, since the returned value is not addressable (example on play):
NewFoo().M1()
When returning a pointer, you can do this. (example on play)
答案2
得分: 3
没有区别。有时候一个版本是“自然的”,有时候是另一个版本。大多数的gophers会更喜欢第一个变体(除非第二个有一些优势)。
(吹毛求疵:Foo{id}
是不好的实践。应该使用 Foo{Id: id}
代替。)
英文:
There is no difference. Sometimes one version is the "natural one", sometimes the other. Most gophers would prefere the first variant (unless the second has some advantages).
(Nitpick: Foo{id}
is bad practice. Use Foo{Id: id}
instead.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论