商君

导航

2018年10月15日 #

Go Example--递归

摘要: ```go package main import "fmt" func main() { fmt.Println(fact(7)) } //函数的递归 func fact(n int) int { if n == 0{ return 1 } return n*fact(n-1) } ``` 阅读全文

posted @ 2018-10-15 19:13 漫步者01 阅读(104) 评论(0) 推荐(0)

Go Example--闭包

摘要: ```go package main import "fmt" func main() { //这里需要将闭包函数当作一个类理解,这里是实例化 nextInt := intSeq() fmt.Println(nextInt()) fmt.Println(nextInt()) fmt.Println(nextInt()) //重新实例化 newInts := intSeq() ... 阅读全文

posted @ 2018-10-15 19:04 漫步者01 阅读(75) 评论(0) 推荐(0)

Go Example--变参函数

摘要: ```go package main import "fmt" func main() { sum(1,2) sum(1,2,3) nums := []int{1,2,3,4} //nums...将nums切片打平为多个参数 sum(nums...) } //定义变参函数 func sum(nums ...int) { fmt.Println(nums," ") tota... 阅读全文

posted @ 2018-10-15 18:47 漫步者01 阅读(95) 评论(0) 推荐(0)

Go Example--函数多返回值

摘要: ```go package main import "fmt" func main() { a,b := vals() fmt.Println(a) fmt.Println(b) } //函数多个返回值 func vals() (int,int) { return 3,7 } ``` 阅读全文

posted @ 2018-10-15 18:44 漫步者01 阅读(104) 评论(0) 推荐(0)

Go Example--函数

摘要: ```go package main import ( "fmt" ) func main() { res := plus(1,2) fmt.Println("1+2=",res) } //定义私有函数,首字母小写。首字母大写,定义公有函数 func plus(a,b int)int { return a+b } ``` 阅读全文

posted @ 2018-10-15 18:42 漫步者01 阅读(94) 评论(0) 推荐(0)

Go Example--range

摘要: ```go package main import "fmt" func main() { nums := []int{2,3,4} sum :=0 //rang 遍历切片 for _,num := range nums { sum += num } fmt.Println("sum:",sum) for i,num := range nums { if num ==... 阅读全文

posted @ 2018-10-15 14:56 漫步者01 阅读(87) 评论(0) 推荐(0)

Go Example--map

摘要: ```go package main import "fmt" func main() { //初始化map make(map[类型][类型]) m:= make(map[string]int) m["k1"]=7 m["k2"]=13 fmt.Println("map:",m) //直接取map中的值,如果key不存在v为对应的零值 v1 := m["k1"] fmt.P... 阅读全文

posted @ 2018-10-15 14:50 漫步者01 阅读(95) 评论(0) 推荐(0)

Go Example--切片

摘要: ```go package main import ( "fmt" ) func main() { //make来初始化一个切片,必须指名切片的长度 s:= make([]string, 3) fmt.Println("emp:",s) s[0] = "a" s[1] = "b" s[2] = "c 阅读全文

posted @ 2018-10-15 14:33 漫步者01 阅读(82) 评论(0) 推荐(0)

Go Example--数组

摘要: ```go package main import "fmt" func main() { //定义一个数组并完成初始化,初始值为对应的零值 var a [5]int fmt.Println("emp:",a) a[4] = 100 fmt.Println("set:",a) fmt.Println 阅读全文

posted @ 2018-10-15 14:17 漫步者01 阅读(73) 评论(0) 推荐(0)