|
|
|
|
|
by 37ef_ced3
1996 days ago
|
|
Thanks for your comment Picking up on one of your snags, Go's slices will "click" for you once you start thinking of it as a pointer and a length (and a capacity), but nothing more. Realize it's just a C struct like this, passed by value: struct intSlice {
int* addr;
int len;
int cap;
};
The memory at addr is not owned by the slice. All the slice operations are simply notation for manipulating the struct. Go's garbage collection makes the whole thing work brilliantlyThis can be confusing if you're used to std::vector (which owns the memory) or python's slices. Go's slices are a shallow pointer/length system exactly like is used in C all the time. For example: void sort(int* addr, int len);
becomes func sort(a []int)
A Go slice is just a pointer/length, with terse notation |
|
The latest thing that made me go "hmm" last time, was this one (I didn't get "bit", just went "hmm" reading it):
https://play.golang.org/p/2bTvXr6WLNN
I'm sure everyone expected this output when using b[1:4], right? All languages do these things a bit differently, so is why I always forget the detailed syntax required. I'm sure there's a valid pragmatic explanation and there's lots of ways to do this (ie. allow negative values etc.). Just a thing that while speedtyping, one could easily miss this little detail.This one is a good intro, but doesn't explain this I believe:
https://blog.golang.org/slices-intro