Constants and Formatting Cheatsheet
Constants Summary
- Declare:
const Name = "Value"
- Rules: Cannot use
:=, must be known at compile time.
- Computed:
const result = 10 * 5 is valid; const time = time.Now() is NOT.
| Verb |
Usage |
Example |
%v |
Any value |
"Value: %v" |
%T |
Type |
float64, string |
%s |
String |
"Hello %s" |
%d |
Integer |
"ID: %d" |
%f |
Float |
3.141590 |
%.2f |
Rounded Float |
3.14 |
%t |
Boolean |
true/false |
Functions
fmt.Printf(): Prints to standard output.
fmt.Sprintf(): Returns the formatted string.
Encoding & Runes
- UTF-8: The default encoding for Go strings.
byte (uint8): Raw 8-bit data.
rune (int32): A single Unicode character (code point).
- Length:
len(s) returns byte count; utf8.RuneCountInString(s) returns character count.