This is a creation in Article, where the information may have evolved or changed.
Basic data types and expressions for the Go language
Basic data types
The data types of go can be summarized as follows:
- Basic type
- integral type : int, int8, Int16, Int32, Int64
- bool: TRUE, False
- floating point : float32, float64
- plural : complex64, complex128
- Errors (Error)
- Constructed type
- Data type ([])
- struct type (struct)
- Pointer Type (*)
declaring Variables
1, integral type (int, int8, Int16, Int32, Int64)
varint1 //定义整型变量i并赋值1varint1 // 也可以这样定义1 // 还可以这样定义
Need to be aware of when assigning an integral type
varint // a是通用整数类型 varint32 // b是32位整数类型/* a、b 的默认初值都为0 */15// b = a 混合类型是非法的,编译不通过1 // 这里需要注意,当定义整型变量时,它们的默认初值是0 // 而1是一个常量,所有它可以执行,执行后b的值是1
In go, constants are created at compile time and can only be numbers, strings, or Boolean values
const1 // 生成常量x,之后不能改变x的值
2, BOOL (true, FALSE)
varbool // 定义bool类型变量b, 默认初值为falsetrue // 赋值
3, floating point (float32, float64)
varfloat32 //默认值是0.0
4. Strings (String)
varstring"hello world"// s[0] = 'y' 非法c := []rune(s) // 使用关键字rune,将s转换为rune数组c[0'c' // 数组的元素是字符string(c) // 创建新的字符串s2
5. Plural (complex128, complex64)
/* 变量类型complex128是64位的虚数,而complex64是32位的虚数 */varcomplex64 = 3 + 4ifmt.Printf("%v\n", c) // 打印c>>>3+4i
6. Errors (Error)
The built-in types that go to use for error reporting
error // 定义一个error类型的变量e, 默认初值是nil