DEV Community

Clavin June
Clavin June

Posted on • Originally published at clavinjune.dev on

Golang Print Struct Verbosely

Sunday Snippet #16 golang print struct verbosely

package main

import (
    "fmt"
)

type Foo struct {
    bar Bar
}

type Bar struct {
    number int
    word   string
}

func NewFoo(number int, word string) *Foo {
    return &Foo{Bar{number, word}}
}

func main() {
    f := NewFoo(10, "testing")
    fmt.Printf("%+v\n%#v\n%v", f, f, f)
}
Enter fullscreen mode Exit fullscreen mode
$ go run main.go
&{bar:{number:10 word:testing}}
&main.Foo{bar:main.Bar{number:10, word:"testing"}}
&{{10 testing}}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)