# go语言渐入佳境[11]-function2

jonson_jackson · · 265 次点击 · · 开始浏览

## 匿名函数

 `123456789101112131415161718192021222324252627282930313233` `package mainimport ( "fmt" "math")//匿名函数func main(){ //无参匿名函数 func(){ fmt.Println("jonson") }() //有参匿名函数 func(data int){ fmt.Println("data:",data) }(5) //有返回值的匿名函数 result:=func(data float64) float64{ return math.Sqrt(data) }(9) fmt.Println("result:",result)  //函数表达式 greet:=func(){ fmt.Println("greet jonson") } greet() fmt.Printf("greet的类型是%T\n",greet)`

## 闭包

 `1234567891011` `func greetjonson(){ x:=0 increment:= func() int{ x++ return x } fmt.Println(increment()) fmt.Println(increment())}`

## 函数作为返回值

 `123456` `func makeGreeter() func() string{ return func() string { return "hello jonson" }}`

## 闭包与函数返回值

 `1234567` `func makeEvenGenerator() func() int{ i:=0 return func() int{ i+=2 return i }}`

## callback函数作为参数

 `123456` `func visit(numbers []int,callback func(int)){ for _,n :=range numbers{ callback(n) }}`

## 递归

 `12345678910111213141516171819202122` `//  计算阶乘     5   5*4*3*2*1func factorial(x uint) uint{ if x==0{ return 1 } return x * factorial(x-1)}// factorial(5)   //120// returns: 5 * factorial(4)// factorial(4)   /24// returns: 4 * factorial(3)// factorial(3)   //6// returns: 3 * factorial(2)// factorial(2)   //2// returns: 2 * factorial(1)// factorial(1)     //1// returns: 1 * factorial(0)// factorial(0)// returns: 1`

## 所有例子

 `123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140` `package mainimport ( "fmt" "math")//匿名函数func main(){ //无参匿名函数 func(){ fmt.Println("jonson") }() //有参匿名函数 func(data int){ fmt.Println("data:",data) }(5) //有返回值的匿名函数 result:=func(data float64) float64{ return math.Sqrt(data) }(9) fmt.Println("result:",result) greet:=func(){ fmt.Println("greet jonson") } greet() fmt.Printf("greet的类型是%T\n",greet) //闭包 greetjonson() //函数作为返回值  makejonson := makeGreeter()  fmt.Printf("类型：%T，返回值：%v\n",makejonson,makejonson()) //闭包做为返回值 makeeven := makeEvenGenerator() fmt.Println("makeeven i = ",makeeven()) fmt.Println("makeeven i = ",makeeven()) fmt.Println("makeeven i = ",makeeven()) //callback函数作为参数 visit([]int{1,2,3,4},func(n int){ fmt.Println(n+1) }) //死循环 fmt.Println(factorial(5)) var i uint = 0 for ;i<20;i++{ fmt.Println(fabicc(i)) }}//闭包func greetjonson(){ x:=0 increment:= func() int{ x++ return x } fmt.Println(increment()) fmt.Println(increment())}//函数作为返回值func makeGreeter() func() string{ return func() string { return "hello jonson" }}//闭包与函数返回值func makeEvenGenerator() func() int{ i:=0 return func() int{ i+=2 return i }}//callback函数作为参数func visit(numbers []int,callback func(int)){ for _,n :=range numbers{ callback(n) }}//递归//  计算阶乘     5   5*4*3*2*1func factorial(x uint) uint{ if x==0{ return 1 } return x * factorial(x-1)}//fabiccfunc fabicc(x  uint) uint{ if(x==0){ return 0 } if(x==1){ return 1 } return fabicc(x-2) + fabicc(x-1)}`
• 版权声明： 本博客所有文章除特别声明外，均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处！

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

## 匿名函数

 `123456789101112131415161718192021222324252627282930313233` `package mainimport ( "fmt" "math")//匿名函数func main(){ //无参匿名函数 func(){ fmt.Println("jonson") }() //有参匿名函数 func(data int){ fmt.Println("data:",data) }(5) //有返回值的匿名函数 result:=func(data float64) float64{ return math.Sqrt(data) }(9) fmt.Println("result:",result)  //函数表达式 greet:=func(){ fmt.Println("greet jonson") } greet() fmt.Printf("greet的类型是%T\n",greet)`

## 闭包

 `1234567891011` `func greetjonson(){ x:=0 increment:= func() int{ x++ return x } fmt.Println(increment()) fmt.Println(increment())}`

## 函数作为返回值

 `123456` `func makeGreeter() func() string{ return func() string { return "hello jonson" }}`

## 闭包与函数返回值

 `1234567` `func makeEvenGenerator() func() int{ i:=0 return func() int{ i+=2 return i }}`

## callback函数作为参数

 `123456` `func visit(numbers []int,callback func(int)){ for _,n :=range numbers{ callback(n) }}`

## 递归

 `12345678910111213141516171819202122` `//  计算阶乘     5   5*4*3*2*1func factorial(x uint) uint{ if x==0{ return 1 } return x * factorial(x-1)}// factorial(5)   //120// returns: 5 * factorial(4)// factorial(4)   /24// returns: 4 * factorial(3)// factorial(3)   //6// returns: 3 * factorial(2)// factorial(2)   //2// returns: 2 * factorial(1)// factorial(1)     //1// returns: 1 * factorial(0)// factorial(0)// returns: 1`

## 所有例子

 `123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140` `package mainimport ( "fmt" "math")//匿名函数func main(){ //无参匿名函数 func(){ fmt.Println("jonson") }() //有参匿名函数 func(data int){ fmt.Println("data:",data) }(5) //有返回值的匿名函数 result:=func(data float64) float64{ return math.Sqrt(data) }(9) fmt.Println("result:",result) greet:=func(){ fmt.Println("greet jonson") } greet() fmt.Printf("greet的类型是%T\n",greet) //闭包 greetjonson() //函数作为返回值  makejonson := makeGreeter()  fmt.Printf("类型：%T，返回值：%v\n",makejonson,makejonson()) //闭包做为返回值 makeeven := makeEvenGenerator() fmt.Println("makeeven i = ",makeeven()) fmt.Println("makeeven i = ",makeeven()) fmt.Println("makeeven i = ",makeeven()) //callback函数作为参数 visit([]int{1,2,3,4},func(n int){ fmt.Println(n+1) }) //死循环 fmt.Println(factorial(5)) var i uint = 0 for ;i<20;i++{ fmt.Println(fabicc(i)) }}//闭包func greetjonson(){ x:=0 increment:= func() int{ x++ return x } fmt.Println(increment()) fmt.Println(increment())}//函数作为返回值func makeGreeter() func() string{ return func() string { return "hello jonson" }}//闭包与函数返回值func makeEvenGenerator() func() int{ i:=0 return func() int{ i+=2 return i }}//callback函数作为参数func visit(numbers []int,callback func(int)){ for _,n :=range numbers{ callback(n) }}//递归//  计算阶乘     5   5*4*3*2*1func factorial(x uint) uint{ if x==0{ return 1 } return x * factorial(x-1)}//fabiccfunc fabicc(x  uint) uint{ if(x==0){ return 0 } if(x==1){ return 1 } return fabicc(x-2) + fabicc(x-1)}`
• 版权声明： 本博客所有文章除特别声明外，均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处！