Specifier | Description | Command | Output |
---|---|---|---|
%+v | Default formatting with field names (for structs) type Point struct { X, Y int } p:= Point{1, 2); | fmt.Printf("%+v", p); | (X:1 Y:2} |
%#v | Go syntax representation | fmt.Printf("%#v", 10) | int (10) |
%v | Default formatting | fmt.Printf("%v", 10) | 10 |
%b | Binary representation | fmt.Printf("%b", 10) | Ob1010 |
%c | Character representation | fmt.Printf("%c", ‘a") | a |
%d | Decimal representation | fmt.Printf(“⅑d”, 10) | 10 |
%0 | Octal representation | fmt.Printf("%o", 10) | 0012 |
%x | Lowercase hexadecimal representation | fmt.Printf("%x", 10) | a |
%X | Uppercase hexadecimal representation | fmt.Printf("%X", 10) | A |
%U | Unicode character “U+XXXX” format | fmt.Printf("%U", ‘€’) | U+20AC |
%u | Unicode character “&#xXXXX;” format | fmt. Printf("%u", ‘€’) | 8#8364; |
%f | Floating-point representation | fmt.Printf("%f", 3.14159) | 3.141590 |
%F | Floating-point representation (capital letter exponent) | fmt.Printf("%F", 3.14159) | 3.141590 |
%g | Floating-point representation (compact format) | fmt.Printf("%g", 3.14159) | 3.14159 |
%G | Floating-point representation (capital letter exponent, compact format) | fmt.Printf("%G", 3.14159) | 3.14159 |
%e | Scientific notation (lowercase exponent) | fmt.Printf("%e", 3.14159) | 3.141590+00 |
%E | Scientific notation (uppercase exponent) | fmt.Printf("%E", 3.14159) | 3.141590+00 |
%s | String representation | fmt.Printf("%s", “Hello”) | Hello |
%p | Pointer representation | var x int = 10 fmt.Printf("%p", &x); | 0x208d40 (example memory address) |
%T | Type of the value | fmt.Printf("%T", 10) | int |
%% | Literal percent sign | fmt.Printf("%%") | % |
Go Format Specifiers
Contribute
Welcome to the cheatsheet collection! This is your chance to contribute and share your knowledge with other developers. Cheatsheets are valuable resources that provide quick reference guides, tips, and tricks for various programming languages, frameworks, and tools. Join me in building a comprehensive collection that empowers developers worldwide.
learn more →