*本来这篇是想写docker的基本操作总结的。。。想想还是写这个吧。。。→_→*
-
变量声明使用 := 方式时,左值必须是未声明的,否则会出现编译错误
//.\main.go:8:4: no new variables on left side of := package main import "fmt" func main() { var a int = 10 var b int = 20 b := 30 fmt.Print(a,b) }
-
变量声明了,就必须要使用,否则会出现编译错误
//.\main.go:7:6: b declared and not used package main import "fmt" func main() { var a int = 10 var b int = 20 fmt.Print(a) }
-
const修饰符的作用范围为同时修饰的所有常量
//.\main.go:7:4: cannot assign to a //.\main.go:8:4: cannot assign to b package main import "fmt" func main() { const a,b int = 10, 20 a = 10 b = 20 fmt.Print(a,b) }
-
++自增和- -自减运算符类比C语言,相当于前置的自增和自减,而且go语言中不区分前置或后置
//.\main.go:9:2: Syntax error: unexpected ++,expecting } package main import "fmt" func main(){ var a int = 10 var b int = 20 ++a b++ fmt.Print(a,b) }
-
不能使用++自增或- -自减运算符初始化变量和对变量赋值
//.\main.go:7:15: Syntax error: unexpected ++ at end of statement //.\main.go:10:7: Syntax error: unexpected ++ at end of statement package main import "fmt" func main(){ var a int = 10 var b int = a++ var c int = 20 c = a++ fmt.Print(a,b,c) }
-
if…else 语句中的 else 必须和 if 的 ’ } ’ 在同一行,否则编译错误
//.\main.go:11:2: Syntax error: unexpected else,expecting } package main import "fmt" func main() { var a int = 30 if a < 20 { fmt.Print("a<20") } else { fmt.Print("a>=20") } }
-
switch 中的 case和default分支不用添加break
//代码运行成功 package main import "fmt" func main() { var a int = 10 switch a { case 1: fmt.Println("1") case 2: fmt.Println("2") case 10: fmt.Println("10") default: fmt.Println("unknow") } }
-
switch 中 也可以不用添加表达式
//代码运行成功 package main import "fmt" func main() { var a int = 10 switch { case a == 1: fmt.Println("1") case a == 2: fmt.Println("2") case a == 10: fmt.Println("10") default: fmt.Println("unknow") } }
-
switch的case分支的常量表达式可以同时测试多个值
//代码运行成功 package main import "fmt" func main() { var a int = 10 switch { case a == 1,a == 2: fmt.Println("1 or 2") case a == 10,a == 20: fmt.Println("10 or 20") case a == 100,a == 200: fmt.Println("100 or 200") default: fmt.Println("unknow") } }
-
switch 语句还可以被用于 type-switch 来判断某个 interface 变量中实际存储的变量类型
package main import "fmt" func main() { var x interface{} switch i := x.(type) { case nil: fmt.Printf("%T",i) case int: fmt.Printf("int") case float64: fmt.Printf("float64") case func(int) float64: fmt.Printf("func(int)") case bool,string: fmt.Printf("bool or string") default: fmt.Printf("unknow") } }
-
select 语句。。这个现在还没看懂。。先MARK
Learning...
-
for循环语句range格式,遍历数组
//i为下标 //x为元素的值 //0 1 //1 2 //2 3 //3 4 //4 5 package main import "fmt" func main() { var number = [5]int{1, 2, 3, 4, 5} for i,x := range number { fmt.Println(i,x) } }
-
for循环语句和C语言中的while循环语句比较
//0 1 2 3 4 5 6 7 8 9 package main import "fmt" func main() { var a int = 0 for a < 10 { fmt.Printf("%d ",a) a++ } }
-
for循环语句和C语言中的for循环语句比较
//0 1 2 3 4 5 6 7 8 9 package main import "fmt" func main() { var a int = 0 for a = 0; a < 10; a++ { fmt.Printf("%d ",a) } }
-
//10 10 //true package main import "fmt" func fun(num1 *int,num2 *int) bool { fmt.Println(*num1,*num2) if *num1 == *num2 { return true } else { return false } } func main() { var a int = 10 var b int = 10 var flag bool = fun(&a,&b) fmt.Print(flag) }
-
函数可以同时返回多个值
//hello world package main import "fmt" func fun(str1 string,str2 string) (string,string) { return str2,str1 } func main() { str1,str2 := fun("world","hello") fmt.Println(str1,str2) }
-
函数可以作为值使用,神奇~
//-1 package main import "fmt" func main() { tmp := func(x int) int { return -x } fmt.Print(tmp(1)) }
-
函数支持匿名函数,可作为闭包。匿名函数是一个”内联”语句或表达式。匿名函数的优越性在于可以直接使用函数内的变量,不必申明。
//1 2 3 package main import "fmt" func first() func() int { i := 0 return func() int { i++ return i } } func main() { tmp := first() fmt.Println(tmp(),tmp(),tmp()) }
-
//10 abc package main import "fmt" //自定义类型 type student struct { num int name string } //方法getNum func (stu student) getNum() int { return stu.num } //方法getName func (stu student) getName() string { return stu.name } func main() { var st student st.num = 10 st.name = "abc" //对象调用其方法 fmt.Println(st.getNum(),st.getName()) }
-
数组的声明、初始化、赋值、访问
//[0 0 0 0 0 0 0 0 0 0] //100 1 2 3 4 5 6 7 8 9 [100 1 2 3 4 5 6 7 8 9] package main import "fmt" func main() { var arr [10]int var number = [...]int{0, 1, 5, 6, 7, 8, 9} number[0] = 100 fmt.Println(arr) var i int = 0 for i = 0; i < 10; i++ { fmt.Printf("%d ",number[i]) } fmt.Println(number) }
-
二维数组的初始化
//[[1 2 3] [4 5 6] [7 8 9]] package main import "fmt" func main() { var number = [3][3]int{ {1,2,3},{4,5,6},{7,8,9}} fmt.Println(number) }
-
//0 1 2 3 4 5 6 7 8 9 10 package main import "fmt" func getNum(number []int) int { var i int = 0 for i = 0; i < 10; i++ { fmt.Printf("%d ",number[i]) } return len(number) } func main() { var number = []int{0, 9} num := getNum(number) fmt.Println(num) }
//0 1 2 3 4 5 6 7 8 9 10 package main import "fmt" func getNum(number [10]int) int { var i int = 0 for i = 0; i < 10; i++ { fmt.Printf("%d ",number[i]) } return len(number) } func main() { var number = [10]int{0, 9} num := getNum(number) fmt.Println(num) }