Literal syntax
Go literal forms for integers, floats, imaginaries, runes, and strings are precise lexical rules, not loose reader-friendly conventions.
Canonical guidance
- Go has distinct literal grammars for integer, floating-point, imaginary, rune, and string forms
- raw strings and interpreted strings serve different purposes
- a rune literal is not a one-character string
Use when
- checking whether a token is valid Go syntax
- reviewing escapes or numeric base syntax
- explaining text and rune handling
Avoid
- assuming single quotes mean strings
- treating raw strings as escape-processing strings
- guessing at numeric literal features without checking the grammar
Preferred pattern
path := `C:\tmp\logs`
Anti-pattern
- using a rune literal where the API expects a string
Explanation: This is tempting when the value has one visible character, but rune and string literals denote different types and semantics.
Why
- lexical literal errors are frequent agent questions because they fail before any deeper type reasoning begins
Related pages
Sources
- The Go Programming Language Specification - Go Team
- Google Go Style Decisions - Google