Reputation: 3865
Whats the difference between &Duck{}
and (*Duck)(nil)
?
Is there any reason to prefer one over the other?
ex:
fmt.Println(reflect.TypeOf(&Duck{}) == reflect.TypeOf((*Duck)(nil)))//true
fmt.Println(nil == (*Duck)(nil))//true
fmt.Println(nil == &Duck{})//false
Upvotes: 1
Views: 109
Reputation: 49255
&Duck{}
points to a "Zero" struct instance, but it is most certainly not nil! You can assign values to it. You can't do all that to a nil pointer, regardless of the fact that they have the same type.
If you're just interested in checking types, I suppose a nil pointer is more efficient as there are no allocations of objects involved.
So it comes down to what exactly it is you want to do.
Upvotes: 2