Hacker News new | ask | show | jobs
by 37ef_ced3 1749 days ago
In Go, a []int ("slice of int") is 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 well.

This can be confusing if you're used to C++'s 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 formalization of C's pointer/length idiom, with terse notation for manipulation.