黄桃
基础语法:
1、iota 用法
func main(){
const {
a = iota //0
b //1
c= 'hello'
d //hello
e = iota //4
h int32 = 10
}
f = iota //0
e = iota //0
fmt.Println(a,b,c,d,e,f)
}
2、==用法
类型不同不能判等
int类型:
func main(){
var aa int = 10//等于C的long 可能是 32 和 64
var bb int32 = 10
var cc int64 = 10
if(aa == bb ){//报错 类型必须相同 但是任何 变量都能和未指定类型的常量判等
}
}
3、:=用法
func main(){
a:=1
}
4、defer 含义
- 1、不管函数异常与否结束时都执行
- 2、defer和顺序有关,一个函数中多个defer,会依次压入栈中,所以执行顺序正好相反
- 3、执行时,函数中所有的代码执行完,再去执行defer,return
package main
import "fmt"
func main() {
fmt.Println("main func_b_0: a = ", func_b_0())//10
fmt.Println("main func_b_1: a = ", func_b_1())//6
fmt.Println("main func_b_2: a = ", func_b_2())//6
fmt.Println("main func_b_3: a = ", func_b_3())//6
}
func func_b_0() (a int) {//必须返回变量a
fmt.Println("func_b_0...top")
a = 5
defer func() {
a = 10
fmt.Println("func_b_0 defer a=", a)//10
}()
a++
fmt.Println("func_b_0_end a = ", a)//6
return a
}
func func_b_1() (int) {//返回的类似为int
fmt.Println("func_b_1...top")
a := 5
defer func() {
a = 10
fmt.Println("func_b_1 defer a=", a)//10
}()
a++
fmt.Println("func_b_1_end a = ", a)//6
return a
}
func func_b_2() int {
fmt.Println("func_b_2...top")
a := 5
defer func(a *int) {
*a = 100
fmt.Println("func_b_2 defer a=", *a)//100
}(&a)
a++
fmt.Println("func_b_2_end a = ", a)//6
return a
}
func func_b_3() int {
fmt.Println("func_b_3...top")
a := 5
defer func(a int) {
a = 100
fmt.Println("func_b_3 defer a=", a)//100
}(a)
a++
fmt.Println("func_b_3_end a = ", a)//6
return a
}
func func_b_4() {//输出 3、2、1
defer fmt.Println("1")
defer fmt.Println("2")
defer fmt.Println("3")
}
有疑问加站长微信联系(非本文作者)