什么时候返回一个指向struct的指针是一个好主意? [英] When is it a good idea to return a pointer to a struct?
问题描述
我正在学Go,我对使用指针的时间有点困惑。特别是,当从一个函数返回一个 struct c>时,什么时候返回结构实例本身是合适的,何时适合返回一个指向结构的指针?
示例代码:
type Car结构{
make string
型号字符串
}
func无论(){
var car汽车
汽车:=汽车{honda,civic }
// ...
返回汽车
}
什么情况下我想返回一个指针,以及我不想 的地方?是否有一个很好的经验法则?
有两件事情你要记住,性能和API。 p>
汽车如何使用?它是具有状态的对象吗?它是一个大型结构?不幸的是,当我不知道汽车是什么时,不可能回答。实际上,最好的办法是看看其他人做什么并复制它们。最终,你会对这种事情有一种感觉。我现在将描述来自标准库的三个示例,并解释为什么我认为他们使用了他们所做的。
-
hash / crc32
:crc32。 NewIEEE()
函数返回一个指针类型(实际上,一个接口,但底层类型是一个指针)。哈希函数的一个实例具有状态。当你将信息写入散列函数时,它会汇总数据,所以当你调用Sum()
方法时,它会给你这个实例的状态。 -
时间
:time.Date
函数返回一个Time
结构。为什么?时间是一个时间。它没有国家。它就像一个整数,你可以比较它们,对它们进行数学运算等。API设计者决定修改时间不会改变当前的一个,而是创建一个新的。作为图书馆的用户,如果我想从现在起一个月的时间,我会想要一个新的时间对象,而不是改变我现有的一个。一段时间也只有3个字的长度。换句话说,它很小,并且在使用指针时不会有任何性能提升。 $ c>: big.NewInt()非常有趣。我们几乎同意,当你修改一个 hash/crc32
: Thecrc32.NewIEEE()
function returns a pointer type (actually, an interface, but the underlying type is a pointer). An instance of a hash function has state. As you write information to a hash, it sums up the data so when you call theSum()
method, it will give you the state of that one instance.time
: Thetime.Date
function returns aTime
struct. Why? A time is a time. It has no state. It is like an integer where you can compare them, preform maths on them, etc. The API designer decided that a modification to a time would not change the current one but make a new one. As a user of the library, if I want the time one month from now, I would want a new time object, not to change the current one I have. A time is also only 3 words in length. In other words, it is small and there would be no performance gain in using a pointer.math/big
: big.NewInt() is an interesting one. We can pretty much agree that when you modify abig.Int
, you will often want a new one. Abig.Int
has no internal state, so why is it a pointer? The answer is simply performance. The programmers realized that big ints are … big. Constantly allocating each time you do a mathematical operation may not be practical. So, they decided to use pointers and allow the programmer to decide when to allocate new space.
big.Int
时,你通常会想要一个新的。一个 big.Int
没有内部状态,为什么它是一个指针?答案只是表现。程序员意识到,大整数是...大。每次进行数学运算时不断分配可能不切合实际。因此,他们决定使用指针,并允许程序员决定何时分配新空间。 题?可能不会。这是一项设计决定,您需要根据具体情况进行判断。当我设计我自己的库时,我使用标准库作为指导。这一切都归结为判断以及您期望客户端代码如何使用您的类型。 I'm learning Go, and I'm a little confused about when to use pointers. Specifically, when returning a struct
from a function, when is it appropriate to return the struct instance itself, and when is it appropriate to return a pointer to the struct?
Example code:
type Car struct {
make string
model string
}
func Whatever() {
var car Car
car := Car{"honda", "civic"}
// ...
return car
}
What are the situations where I would want to return a pointer, and where I would not want to? Is there a good rule of thumb?
There are two things you want to keep in mind, performance and API.
How is a Car used? Is it an object which has state? Is it a large struct? Unfortunately, it is impossible to answer when I have no idea what a Car is. Truthfully, the best way is to see what others do and copy them. Eventually, you get a feeling for this sort of thing. I will now describe three examples from the standard library and explain why I think they used what they did.
Have I answered your question? Probably not. It is a design decision and you need to figure it out on a case by case basis. I use the standard library as a guide when I am designing my own libraries. It really all comes down to judgement and how you expect client code to use your types.
这篇关于什么时候返回一个指向struct的指针是一个好主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!