在Go模板中打印sql.NullString的值

I have fetched details from database where couple of column were of sql.NullString and sql.NullInt64 column.

Now, while I print them, after checking if it is Valid, it prints data in {3 true} format. I only want to print value 3 from it.

How can I achieve this?

Currently this is what, I am printing:

{{ range $value := .CatMenu }}
   ... // Other data
   {{ $value.ParentID }}  // This is sql.NullInt64 type
{{ end }}

sql.NullInt64 is a struct:

type NullInt64 struct {
    Int64 int64
    Valid bool // Valid is true if Int64 is not NULL
}

When printing struct values, the default formatting is what you currently see.

If you check if it is valid and non-nil prior, you can simply print the NullInt64.Int64 field only which holds the numerical value.

This is how you can do it:

{{ range $value := .CatMenu }}
   ... // Other data
   {{ $value.ParentID.Int64 }}  // This is sql.NullInt64 type
{{ end }}

See this simple example to test it:

vs := []*sql.NullInt64{
    {3, true},
    {2, true},
}

t := template.Must(template.New("").Parse(
    "{{range .}}{{.Int64}}
{{end}}",
))

if err := t.Execute(os.Stdout, vs); err != nil {
    panic(err)
}

Output (try it on the Go Playground):

3
2