|
|
|
|
|
by pstuart
501 days ago
|
|
Only really a gotcha if you pass a slice into a function and expect to see modifications in that slice after the function completes. It's helpful to remember that Go passes by value, not reference. 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).
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:
I'm sure the output makes perfect intuitive sense https://go.dev/play/p/79gOzSStTp4