Skip to content

Instantly share code, notes, and snippets.

@k-sone
Created March 3, 2015 06:24
Show Gist options
  • Save k-sone/196fd78a8c87f5c8e612 to your computer and use it in GitHub Desktop.
Save k-sone/196fd78a8c87f5c8e612 to your computer and use it in GitHub Desktop.
examples of fmt.Printf

src:

package main

import "fmt"

type A struct {
	Name   string
	Number int
	Enable bool
	Next   *A
}

func printf(title string, a interface{}) {
	fmt.Printf("--- %s ---\n", title)
	fmt.Printf("%%v  : %v\n", a)
	fmt.Printf("%%+v : %+v\n", a)
	fmt.Printf("%%#v : %#v\n", a)
	fmt.Printf("%%T  : %T\n", a)
	fmt.Printf("%%s  : %s\n", a)
}

func main() {
	printf("bool", true)
	printf("int", 1)
	printf("string", "aa")
	printf("nil", nil)

	a := A{"Foo", 10, true, &A{"Bar", 20, false, nil}}
	printf("struct", a)
}

result:

--- bool ---
%v  : true
%+v : true
%#v : true
%T  : bool
%s  : %!s(bool=true)
--- int ---
%v  : 1
%+v : 1
%#v : 1
%T  : int
%s  : %!s(int=1)
--- string ---
%v  : aa
%+v : aa
%#v : "aa"
%T  : string
%s  : aa
--- nil ---
%v  : <nil>
%+v : <nil>
%#v : <nil>
%T  : <nil>
%s  : %!s(<nil>)
--- struct ---
%v  : {Foo 10 true 0xc208024180}
%+v : {Name:Foo Number:10 Enable:true Next:0xc208024180}
%#v : main.A{Name:"Foo", Number:10, Enable:true, Next:(*main.A)(0xc208024180)}
%T  : main.A
%s  : {Foo %!s(int=10) %!s(bool=true) %!s(*main.A=&{Bar 20 false <nil>})}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment