by eu · 1y ago · 0 comments
That can be addressed by passing the slice as a pointer: https://go.dev/play/p/h9Cg8qL9kNL
Slices are passed partly by value (the length), partly by reference (the data).
func takeSlice(s []int) {
slices.Sort(s)
}
From your explanation, you would expect that to not mutate the slice passed in, but it does.This can have other quite confusing gotchas, like:
func f(s []int) {
_ = append(s, 1)
}
func main() {
s := []int{1, 2, 3}
f(s[0:2])
fmt.Printf("%v\n", s)
}
I'm sure the output makes perfect intuitive sense https://go.dev/play/p/79gOzSStTp4While most people highlight the difficulty of picking up the syntax, I find Rust to be an incredibly tedious language overall. Zig has a less expressive type system, but it compiles much faster (though not as fast as Go). I like what Zig and Odin folks are doing over there.
I like the balance Go strikes between developer productivity and power, though I dearly miss union types in Go.