table of Contents
Literal
The symbols that represent fixed values in programming language source programs are called literal quantities, or literal constants. Generally, a sequence of bare characters is used to represent different types of values. Literals can be directly converted to a value of a certain type by a programming language compiler. Go literals can appear in two places: one is used for the initialization of constants and variables, and the other is used in expressions as function call arguments. If the variable type is not explicitly specified in the variable initialization statement, the Go compiler will automatically perform type inference based on the literal value. Literals in Go can only express values of basic types, and Go does not support user-defined literals.
Literal type
There are several types of literals:
Integer literal
Integer literals use specific character sequences to represent specific integer values. , Often used for the initialization of integer variables or constants. E.g:
42
0X6F
Floating-point literal
Floating-point literals use a specific sequence of characters to represent a floating-point value. It supports two formats: one is the standard mathematical decimal form, such as 0.23; the other is scientific notation, such as 1E6.
3.61 // 数学小数形式
3E2 // 科学计数法
Plural type literal
Plural type literals use a specific sequence of characters to represent the constant value of the plural type.
0i
011i
0.i
2.123i
1.e+0i
5.123-11i
.25i
Character literal
Go's source code uses UTF-8 encoding, and UTF-8 characters occupy 1 to 4 bytes. Go characters are wrapped in a pair of single quotes.
'a'
'本'
'\n'
'\000'
'\x0f'
'\u12e4'
String literal
The representation of string literal in Go is the character literal or its coded value wrapped in a pair of double quotation marks or a pair of "`".
"\n"
"\""
`"`
"Hi, Golang!"
"今天天气不错"