I worked with C
and C
for a while before starting to learn go, and I'm curious why *int
and []int
are treated as different types in golang. Whether you want to think of it as an array or not is up to you, but they should both be pointers to some location in memory indicating the beginning of a list of type int. That list may very well be of size one, but my point is, why are []int
and *int
not the same thing in go?
CodePudding user response:
An []int
has three values internally: pointer to backing array, length of backing array and capacity of backing array. The Go runtime ensures that the application does index outside the bounds of the backing array.
An *int
is just a pointer as in C. Because Go does not have pointer arithmetic (outside of the unsafe package), a *int
cannot be used like an array as in C.