func main() { a := 10 b := 20 defer func(a, b int) { fmt.Println("in defer func a=", a) fmt.Println("in defer func b=", b) }(a, b) //只是传入参数,但未执行,所以执行结果是a=10,b=20,都是未改变的值
a = 110 b = 220 fmt.Println("in main a=", a) fmt.Println("in main b=", b) }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package main
import ( "fmt" )
func main() { a := 10 b := 20 defer func() { fmt.Println("in defer func a=", a) fmt.Println("in defer func b=", b) }() // 匿名调用 ,调用的是最近变量,所以这里是调用的改变后的a,b的值:110,220
a = 110 b = 220 fmt.Println("in main a=", a) fmt.Println("in main b=", b) }
获取取命令行参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package main
import ( "fmt" "os" )
func main() { list := os.Args for i, data := range list { fmt.Printf("os.Args[%v]= %v\n", i, data) }
}
包的导入
导入的包必须使用,否则编译不能通过
给导入的包起别名
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package main
import ( io "fmt" //别名放于包名之前,用空格分隔 "os" )
func main() { list := os.Args for i, data := range list { io.Printf("os.Args[%v]= %v\n", i, data) }