Everything is beautiful in #golang:"Arrays have their place, but they’re a bit inflexible, so you don’t see them too often in Go code."What for and why they designed Array then?#scentmark
-
Everything is beautiful in #golang:
"Arrays have their place, but they’re a bit inflexible, so you don’t see them too often in Go code."
What for and why they designed Array then?
-
undefined fosstodon.org ha condiviso questa discussione
-
Everything is beautiful in #golang:
"Arrays have their place, but they’re a bit inflexible, so you don’t see them too often in Go code."
What for and why they designed Array then?
1. arrays are allocated on stack, not heap. slices cannot be allocated on stack because their size is not statically known.
2. array size is static and part of the type. taking a[4] from 2-element array is compilation error.
3. a[0] on array usually produces faster and safer code because boundary check is static.
4. arrays power slices.
use arrays when all items are the same type and their number is known. you'll know when you can. for example, matrices in a 3D game engine.
-
1. arrays are allocated on stack, not heap. slices cannot be allocated on stack because their size is not statically known.
2. array size is static and part of the type. taking a[4] from 2-element array is compilation error.
3. a[0] on array usually produces faster and safer code because boundary check is static.
4. arrays power slices.
use arrays when all items are the same type and their number is known. you'll know when you can. for example, matrices in a 3D game engine.
a point in 3D space would be a struct{x, y, z int} but a vector in 100-dimension space would be [100]int.
-
1. arrays are allocated on stack, not heap. slices cannot be allocated on stack because their size is not statically known.
2. array size is static and part of the type. taking a[4] from 2-element array is compilation error.
3. a[0] on array usually produces faster and safer code because boundary check is static.
4. arrays power slices.
use arrays when all items are the same type and their number is known. you'll know when you can. for example, matrices in a 3D game engine.
@orsinium 1 sounds contradictory, esp. along with 4. Anyway, pet does not care about allocation issues at the moment. It will learn that later.
100-dimension space in a very rare case. If pet was a mathematician it would prefer variable dimensions.
-
@orsinium 1 sounds contradictory, esp. along with 4. Anyway, pet does not care about allocation issues at the moment. It will learn that later.
100-dimension space in a very rare case. If pet was a mathematician it would prefer variable dimensions.
[4]int is on stack, *[4]int is on heap. slices are powered by *[N]T arrays.
using arrays directly is rare, that's the point. however, having them in the language is essential for the language to work.