In the Go
codebase I am looking at, I frequently see stuff like the following:
some_func(&SomeStructType{
some_attribute: get_ptr(some_object.some_var),
....
)}
func get_ptr[T any](val T) *T {
return &val
}
I don’t understand why get_ptr
is needed. Is there any material difference between this and
some_func(&SomeStructType{
some_attribute: &(some_object.some_var),
....
)}
?
I’ve been reading a bit on garbage collection and heap/stack allocation in Go, and think I have the basics down, but I still don’t grasp why the helper would be needed here.
1