string-format-go

path: courses/go-course/string-format-go.md

- **fileName**: string-format-go
- **Created on**: 2024-08-23 20:20:38

String Format Specifiers in Go

Format Specifier Description Example Output
%v Default format (any value) fmt.Printf("%v", 123) 123
%+v Adds field names to structs fmt.Printf("%+v", struct { Name string }{"Alice"}) {Name:Alice}
%#v Go-syntax representation fmt.Printf("%#v", struct { Name string }{"Alice"}) struct { Name string }{Name:"Alice"}
%T Type of the value fmt.Printf("%T", 123) int
") %
%t Boolean fmt.Printf("%t", true) true
%b Binary representation fmt.Printf("%b", 5) 101
%c Character (rune) fmt.Printf("%c", 65) A
%d Decimal integer fmt.Printf("%d", 123) 123
%o Octal integer fmt.Printf("%o", 123) 173
%x Hexadecimal (lowercase) fmt.Printf("%x", 255) ff
%X Hexadecimal (uppercase) fmt.Printf("%X", 255) FF
%e Scientific notation (lowercase) fmt.Printf("%e", 1234.5678) 1.234568e+03
%E Scientific notation (uppercase) fmt.Printf("%E", 1234.5678) 1.234568E+03
%f Floating-point number fmt.Printf("%f", 1234.5678) 1234.567800
%.2f Floating-point with 2 decimals fmt.Printf("%.2f", 1234.5678) 1234.57
%g Shortest representation (%e or %f) fmt.Printf("%g", 1234.5678) 1234.5678
%G Shortest representation (%E or %f) fmt.Printf("%G", 1234.5678) 1234.5678
%s String fmt.Printf("%s", "hello") hello
%q Double-quoted string with escapes fmt.Printf("%q", "hello") "hello"
%p Pointer address fmt.Printf("%p", &[]int{1, 2, 3}) 0xc00000a0b0
%U Unicode format: U+1234 fmt.Printf("%U", 'A') U+0041

continue:variable-declaration.md
before:fmt.md