go language does not support implicit type conversion, aliases, and original type can not be implicit type conversion
go language does not support implicit conversion
variable
Variable declaration
var v1 int
var v2 string
var v3 [10]int // 数组
var v4 []int // 数组切片
var v5 struct { //定义结构体(Java中的实体类,自定义类型)
f int
}
var v6 *int // 指针
var v7 map[string] int // map,key为string类型,value为int类型
var v8 func(a int) int
//也可以这样声明变量
var (
v1 int
v2 string
)
Declare variables do not need to use a semicolon as a terminator
Variable initialization
var v1 int = 10 // 定义并赋值
var v2 = 10 // 编译器可以自动推导出v2的类型
v3 := 10 // 编译器可以自动推导出v3的类型
A combination of a colon and an equal sign: = assignment statement and
Appears in: variable = Left should not have been declared before, otherwise it will cause a compiler error
var int
a := 2
It will cause a compile error similar to the following:
NO new new left Side of the Variables ON: =
Assignment of variables
var v10 int #定义变量
v10 = 1 #变量赋值
go language provides multiple assignment function, such as the following exchange statement i and j variables:
i, j = j, i
In the assignment does not support multiple languages, the content interaction of two variables need to introduce an intermediate variable:
T = I; J = I; T = J;
Anonymous variable
When we use the traditional strongly typed programming language, which is often the case that in order to obtain a value, but because the function return multiple values and had a bunch of useless variables defined in the function call. In this case Go can return multiple variables by using anonymous binding and to avoid writing this ugly, make the code look more elegant.
Suppose GetName () function is as follows, it returns three values, respectively firstName, lastName and nickName:
func GetName() (firstName, lastName, nickName string) {
return "May", "Chan", "Chibi Maruko"
}
If just want to get nickName, the function call statement can be written as follows:
, , NICKNAME: = GetName ()
This usage makes the code very clear, basically block out the contents of the code reader may be confused line of sight, thereby significantly reducing the complexity of the difficulty of communication and code maintenance.
constant
Constants are known and will not be changed during compilation value. It may be a constant value types (including integer, floating point and complex type), a Boolean type, a string type.
Literals
Hard-coded program constants
-12
3.14159265358979323846 // 浮点类型的常量
3.2+12i // 复数类型的常量
true // 布尔类型的常量
"foo" // 字符串常量
Go literal language is closer to the concept of our constant natural language, which is untyped. As long as the constants in the range of values of the corresponding type, the type can be used as a constant
Constant Defined
Const keyword to define constants
const Pi float64 = 3.14159265358979323846
const zero = 0.0 // 无类型浮点常量
const (
size int64 = 1024
eof = -1 // 无类型整型常量
)
const u, v float32 = 0, 3 // u = 0.0, v = 3.0,常量的多重赋值
const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo", 无类型整型和字符串常量
//常量赋值是一个编译期行为,所以右值也可以是一个在编译期运算的常量表达式:
const s = 1000 * 60
Predefined Constants
Go language these predefined constants: true, false and iota.
iota rather special, can be considered a modified compiler constants, is reset to zero at every const keyword appears, and then appear before a const next, occur once every iota, they represent numbers automatically incremented.
const ( // iota被重设为0
c0 = iota // c0 == 0
c1 = iota // c1 == 1
c2 = iota // c2 == 2
)
const (
u = iota * 42 // u == 0
v float64 = iota * 42 // v == 42.0
)
**** If the code word is not easy to have to help you Give me a concern ****
**** love life love technology QQ group: 894 109 590 ****