Tipi e valori
Naturalmente Go ha più tipi e valori che possono essere assegnati a ciascun tipo.
Per esempio:
(020values.go):
package main
import "fmt"
func main() {
// Concatenazione di stringhe con l'operatore +
fmt.Println("go" + "lang")
// Interi e float
fmt.Println("1+1 =", 1+1)
fmt.Println("7.0/3.0 =", 7.0/3.0)
// Booleani con operatori AND, OR e NOT
fmt.Println(true && false)
fmt.Println(true || false)
fmt.Println(!true)
}
Le costanti stringa sono racchiuse tra doppi apici o apici singoli rovesciati. Le costanti numero sono float a 64 bit di precisione se hanno il punto decimale o l'esponente. Le costanti booleane hanno il valore true
o false
.
Letterali
La seguente è una piccola sintassi di Backus-Naur per i token letterali di Go.
Letterali interi
int_lit = decimal_lit | octal_lit | hex_lit .
decimal_lit = ( "1" … "9" ) { decimal_digit } .
octal_lit = "0" { octal_digit } .
hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit }
Letterali float
float_lit = decimals "." [ decimals ] [ exponent ] |
decimals exponent |
"." decimals [ exponent ] .
decimals = decimal_digit { decimal_digit } .
exponent = ( "e" | "E" ) [ "+" | "-" ] decimals
Letterali immaginari
imaginary_lit = (decimals | float_lit) "i"
Letterali per rune
Una runa è il carattere, non la sua rappresentazione in codice Unicode.
rune_lit = "'" ( unicode_value | byte_value ) "'" .
unicode_value = unicode_char | little_u_value | big_u_value |
escaped_char .
byte_value = octal_byte_value | hex_byte_value .
octal_byte_value = `\` octal_digit octal_digit octal_digit .
hex_byte_value = `\` "x" hex_digit hex_digit .
little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
hex_digit hex_digit hex_digit hex_digit .
escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` )
Caratteri speciali
\a U+0007 alert or bell
\b U+0008 backspace
\f U+000C form feed
\n U+000A line feed or newline
\r U+000D carriage return
\t U+0009 horizontal tab
\v U+000b vertical tab
\\ U+005c backslash
\' U+0027 single quote (valid escape only within rune literals)
\" U+0022 double quote (valid escape only within string literals)