package main
import "fmt"
var num int = test()
func test() int {
fmt.Println("test函数被调用执行")
return 10
}
func init() {
fmt.Println("init函数被调用执行")
}
var t1 = 111 //全局变量
var t2 = "sdskdjsk"
func main() {
var age int //局部变量
age = 1 //声明1个变量
fmt.Println("age= ", age)
var sex string = "男"
fmt.Println(sex)
var pai float64 = 3.1415926
fmt.Println(pai)
var (
a1 = 1
b2 = "cvv"
c3 = 3.14
) //声明多个变量
fmt.Println(a1, b2, c3)
var t5, t6, t3, t4 int
fmt.Print(t5)
fmt.Print(t6)
fmt.Print(t4)
fmt.Print(t3)
var score = 90
if score >= 90 {
fmt.Println("优秀")
} else if score >= 80 {
fmt.Println("良好")
} else if score >= 60 {
fmt.Println("及格")
} else {
fmt.Println("不及格")
}
for i := 1; i < 5; i++ {
fmt.Println(i)
}
var str string = "dsfksjsjfsldjfkdjflsd"
for i, v := range str {
fmt.Println(i, v)
}
var arr = [...]int{1, 2, 3, 4}
for k,v:=range arr{
fmt.Println(k,v)
}
}
运行结果
test函数被调用执行
init函数被调用执行
age= 1
男
3.1415926
1 cvv 3.14
0000优秀
1
2
3
4
0 100
1 115
2 102
3 107
4 115
5 106
6 115
7 106
8 102
9 115
10 108
11 100
12 106
13 102
14 107
15 100
16 106
17 102
18 108
19 115
20 100
0 1
1 2
2 3
3 4