Overview
Before you declare a variable, we should first understand the data types of variables under this article mainly related to the string, boolean, numeric, other types behind the opening talk.
Data type
String
string
Only a pair of double quotation marks ( "") or trans quotation marks ( '') enclose definition can not ( '') is defined by a single quote!
Boolean
bool
Only true and false, default is false.
Digital
integer
int8 uint8 int16 uint16 int32 uint32 int64 uint64
int uint, a specific length of digits depends on the CPU.
Float
float32 float64
Constant declaration
constant at compile stage finalized values, and the program can not change the value at runtime.
Single constant declaration
first: const Variable Name Data Type = variable value
if no assignment, uses the default value of the data type.
The second: const variable name = variable value
according to the value of the variable, the data type itself is determined.
A plurality of constant declaration
first: const variable name, the name ..., Data Type = variable value, the variable value ...
second: const variable name, variable name = ... variable value, the variable value .. .
test code
//demo_1.go package main import ( "fmt" ) func main() { const name string = "Tom" fmt.Println(name) const age = 30 fmt.Println(age) const name_1, name_2 string = "Tom", "Jay" fmt.Println(name_1, name_2) const name_3, age_1 = "Tom", 30 fmt.Println(name_3, age_1) }
Run Results:
Variable declaration
single variable declaration
first: var = variable name data type variable value
if no assignment, uses the default value of the data type.
The second: var variable name = variable value
according to the value of the variable, the data type itself is determined.
Third: variable name: variable value =
omitted var and data types, it must be a variable name is not declared.
A plurality of variable declaration
first: var variable name, the name ..., Data Type = variable value, the variable value ...
second: var variable name, variable name = ... variable value, the variable value .. .
third: variable name, the name ...: = variable value, the variable value ...
test code
//demo_2.go package main import ( "fmt" ) func main() { was age_1 uint8 = 31 was age_2 = 32 age_3 := 33 fmt.Println(age_1, age_2, age_3) var age_4, age_5, age_6 int = 31, 32, 33 fmt.Println(age_4, age_5, age_6) var name_1, age_7 = "Tom", 30 fmt.Println(name_1, age_7) name_2, is_boy, height := "Jay", true, 180.66 fmt.Println(name_2, is_boy, height) }
Run Results:
output method
fmt.Print: to the console (but outputs only)
fmt.Println: to the console and wrap
fmt.Printf: only outputs the formatted string and a string variable (integer and integer variables not)
fmt.Sprintf: returns a formatted string and is not output.
Test code
//demo_3.go package main import ( "fmt" ) func main() { fmt.Print ( "output to the console does not wrap") fmt.Println("---") fmt.Println ( "output to the console and wrap") fmt.Printf("name=%s,age=%d\n", "Tom", 30) fmt.Printf("name=%s,age=%d,height=%v\n", "Tom", 30, fmt.Sprintf("%.2f", 180.567)) }
operation result: