Notessh2a

String Formatting

PlaceholderDescriptionUsageOutput
%vDefault format for any value.("%v", 123)123
%+vStruct format with field names.("%+v", Person{Name: "John", Age: 35}){Name:John Age:35}
%#vGo syntax representation of the value.("%#v", []int{1, 2, 3})[]int{1, 2, 3}
%sString without quotes.("%s", "hello")hello
%qQuoted string.("%q", "hello")"hello"
%dInteger in base 10.("%d", 123)123
%bInteger in base 2.("%b", 123)1111011
%oInteger in base 8.("%o", 123)173
%xInteger in base 16 with lowercase letters.("%x", 123)7b
%XInteger in base 16 with uppercase letters.("%X", 123)7B
%cInteger in Unicode code point.("%c", 65)A
%fDecimal format for floating-point numbers.("%f", 123.456)123.456000
%eScientific notation with lowercase e.("%e", 123.456)1.234560e+02
%EScientific notation with uppercase E.("%E", 123.456)1.234560E+02
%tBoolean value.("%t", true)true
%pHexadecimal memory address.("%p", &x)0xc000098040
%TType of the value.("%T", 123)int
%%Literal % sign.("%%")%

Examples:

name := "John"
age := 35
char := 97
fmt.Printf("Name: %s, Age: %d, Char: %c ", name, age, char) // Name: John, Age: 35, Char: a
pi := 3.14159
fmt.Printf("Pi: %.2f", pi) // Pi: 3.14