Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Golang String Formatting

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…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading