package main
import ("fmt")
func main() {
var bts []byte =nil
fmt.Println("the result :", string(bts)) // why not panic ?
// the result :
}
https://play.golang.org/p/dzRzzKvUyd
Because zero value of a slice (nil) acts like a zero-length slice. E.g. you also can declare a slice variable and then append to it in a loop:
// Filter returns a new slice holding only
// the elements of s that satisfy f()
func Filter(s []int, fn func(int) bool) []int {
var p []int // == nil
for _, v := range s {
if fn(v) {
p = append(p, v)
}
}
return p
}
More details can be found here: https://blog.golang.org/go-slices-usage-and-internals
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With