7 ms·
Understanding Slices in Go
- deleted 13y ago[deleted]
- danieldk 13y agoHmm, so they're just safe pointers with bound checking. Cool, you can actually type safely slice apart an array. They handled that much better than C. Isn't nearly any implementation of arrays in other languages? E.g. ArrayList in Java or vector in Haskell. If a language uses garbage collection, chances are that views/slices are also supported.
- pjmlp 13y agoYes, the slices concept is not new, many languages have them. It baffles me that so many Go newbies think of them as a Go specific novelty.
- seanmcdirmid 13y agoI added sliced array projections to Scala back in 2006. Have no idea if they are still there after Martin's collection API overhaul given the focus on immutable data structures and functional behavior.
- chewxy 13y agoslices are not arrays in Go. Slices are built on top of arrays. You can still access Go arrays.
- dalek_cannes 13y agoalso STL vector in C++?
- voyou 13y agoAIUI, multiple Go slices can access the same underlying array, whereas each C++ vector has its own storage. The C++ equivalent to a Go slice would be a pair of iterators. C++'s system is more flexible, though; I think you can only obtain Go slices over arrays, but C++ iterators are used to represent views over all collection types (linked lists, maps, etc).
- vanderZwan 13y ago> C++'s system is more flexible, though Well you are comparing templates to non-templates...
- Djehngo 13y agoI think the difference between go slices and java ArrayLists is that go allows you direct access to the underlying array.
- grannyg00se 13y agoI often see this argument coming about. Someone talks about how great language E is because it has feature X. Then someone else comes along and says languages A, B, C, and D also have X. As though that somehow diminishes the value of having feature X in E. Go doesn't try to be full of fancy new ideas. Most of the ideas come from other languages. Go tries to be the best collection of known ideas that it can be, examining benefits and tradeoffs each step of the way and giving sound reasoning for each decision. Recently I found myself slightly annoyed that they use hg instead of git. Sure enough, I found an explanation in the FAQ. http://golang.org/doc/faq http://golang.org/doc/faq
- AYBABTME 13y agoUnderstanding slices : - slice.c, http://golang.org/src/pkg/runtime/slice.c - Dynamic arrays, https://en.wikipedia.org/wiki/Dynamic_array