I have the below code,
package main
import "fmt"
func main() {
var s1 []int
//s2 := []int{}
fmt.Printf("%d, %d, %T, %t, %#[3]v\n", len(s1), cap(s1), s1, s1 == nil)
}
and the output is,
0, 0, []int, true, []int(nil)
Im really confused with the use of "%[3]v",.. since Im passing only 4 arguments…
The output I was expecting is
0, 0, []int, true
But "[]int(nil)" in the output is confusing me a bit.
>Solution :
If u want to get 0, 0, []int, true, pls use this:
fmt.Printf("%d, %d, %T, %t\n", len(s1), cap(s1), s1, s1 == nil)
Your code:
fmt.Printf("%d, %d, %T, %t, %#[3]v\n", len(s1), cap(s1), s1, s1 == nil)
%#[3]v refers to the third argument, which is s1. []int(nil) is the default representation of an empty slice of integers.