|
|
|
|
|
by saghm
1094 days ago
|
|
Okay, yeah, that definitely isn't what I expected. It's pretty wild to me that `s = s[:2]` will ever work fine if `len(s) == 1`; I would have assumed that it would always be the same regardless of how the slice was created. Playing around with it, it seems like this means that if you pass a subslice to a function, that function can get access to things from the entire slice, including the portions that weren't in the slice passed in[1]! I think I understand now why `clear` can't work on slices the way I think it should, but only because slices themselves don't work the way I feel even stronger that they should. [1]: https://play.golang.com/p/fYrGUbCePuD |
|
Anyhow, this explains it in detail, if you're not already familiar with it: https://go.dev/blog/slices-intro