|
|
|
|
|
by _y5hn
1986 days ago
|
|
For me I just use what's available and do not go into too much detail beyond what I need. However, slices can be confusing at first, and your above example could help think of them the right way. Although, until you get "bit", you might not deduct the consequences of slices right away. Especially when accustomed to other languages. The latest thing that made me go "hmm" last time, was this one (I didn't get "bit", just went "hmm" reading it): https://play.golang.org/p/2bTvXr6WLNN package main
import (
"fmt"
)
func main() {
b := []byte{'g', 'o', 'l', 'a', 'n', 'g'}
fmt.Println(string(b[1:4]))
// Output:
// ola
}
I'm sure everyone expected this output when using b[1:4], right? All languages do these things a bit differently, so is why I always forget the detailed syntax required. I'm sure there's a valid pragmatic explanation and there's lots of ways to do this (ie. allow negative values etc.). Just a thing that while speedtyping, one could easily miss this little detail.This one is a good intro, but doesn't explain this I believe: https://blog.golang.org/slices-intro |
|