博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
go anonymous function
阅读量:6989 次
发布时间:2019-06-27

本文共 1379 字,大约阅读时间需要 4 分钟。

package main

import "fmt"

// function add

func add(a, b int) int {
return a + b
}

// 1

func testFunc1() {

// function "add" to var "f1"// then "f1" is a functionf1 := add// type of f1 = func(int int) intfmt.Printf("type of f1 = %T\n", f1)// call function "f1"// params are 2 and 5sum := f1(2, 5)// sum = 7fmt.Printf("sum = %d\n", sum)

}

// 2

func testFunc2() {

// anonymous function to var "f1", then "f1" is a functionf1 := func(a, b int) int {    return a + b}// type of f1 = function(int, int) intfmt.Printf("type of f1 = %T\n", f1)// call function f1, params are 2 and 5sum := f1(2, 5)// sum = 7fmt.Printf("sum = %d\n", sum)

}

// 3

func testFunc3() {
var i = 0

// the statement after "defer" will be pushed into stack first// so the value of var "i" will be "0"// defer i = 0defer fmt.Printf("defer i = %d\n", i)i = 100// i = 100fmt.Printf("i = %d\n", i)return

}

// 4

func testFunc4() {
var i = 0

// the anonymous function after "defer" will be pushed into stack first// but at this time, the statement in function will not be pushed into stack// so at this time the value of var "i" is not specific// the value of var// at the end the value of var "i" is 100defer func() {    fmt.Printf("defer i = %d\n", i)}()i = 100// i = 100fmt.Printf("i = %d\n", i)return

}

func main() {

//testFunc1()
//testFunc2()
//testFunc3()
testFunc4()
}

转载于:https://blog.51cto.com/11317783/2066501

你可能感兴趣的文章
lombok使用方法
查看>>
多线程基础
查看>>
1028: C语言程序设计教程(第三版)课后习题8.2
查看>>
批量更新软连接脚本
查看>>
Linux 文件和目录的属性
查看>>
Log4j配置使用
查看>>
初步认识Hadoop
查看>>
jQuery对象扩展方法(Extend)深度解析
查看>>
9道前端技能编程题
查看>>
NOIP 2000年提高组复赛 单词接龙
查看>>
mysql-索引与优化
查看>>
sql server 2008安装需要一直重启。但重启后又没有达到效果。
查看>>
Psp个人软件开发工具
查看>>
uva 1395(kruskal变形)
查看>>
斜率优化
查看>>
php 比较运算符
查看>>
for循环效率问题求解答
查看>>
Android so lib库远程http下载和动态注册
查看>>
痞子衡嵌入式:并行接口NAND标准(ONFI)及SLC Raw NAND简介
查看>>
Q-criterion- definition and post-processing
查看>>