This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 应用闭包:将函数作为返回值 | |
| // 工厂函数 | |
| func MakeAddSuffix(suffix string) func(string) string { | |
| return func(name string) string { | |
| if !strings.HasSuffix(name, suffix) { | |
| return name + suffix | |
| } | |
| return name | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 在需要的时候实现一个 where() 闭包函数来打印函数执行的位置: | |
| where := func() { | |
| _, file, line, _ := runtime.Caller(1) | |
| log.Printf("%s:%d", file, line) | |
| } | |
| where() | |
| // some code | |
| where() | |
| // some more code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func main() { | |
| result := 0 | |
| for i := 0; i <= 10; i++ { | |
| result = fibonacci(i) | |
| fmt.Printf("fibonacci(%d) is: %d\n", i, result) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //下面的代码展示了另一种在调试时使用 defer 语句的手法(示例 6.12 defer_logvalues.go): | |
| package main | |
| import ( | |
| "io" | |
| "log" | |
| ) | |
| func func1(s string) (n int, err error) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func trace(s string) string { | |
| fmt.Println("entering:", s) | |
| return s | |
| } | |
| func un(s string) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func main() { | |
| doDBOperations() | |
| } | |
| func connectToDB() { | |
| fmt.Println("ok, connected to db") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //6.3 传递变长参数 | |
| func typecheck(..,..,values … interface{}) { | |
| for _, value := range values { | |
| switch v := value.(type) { | |
| case int: … | |
| case float: … | |
| case string: … | |
| case bool: … | |
| default: … | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for ix, val := range coll { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // for 初始化语句; 条件语句; 修饰语句 {} | |
| for i := 0; i < 5; i++ { | |
| fmt.Printf("This is the %d iteration\n", i) | |
| } | |
| /* | |
| 初始化语句 | |
| for 条件语句 { | |
| 修饰语句 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| switch i { | |
| case 0: // 空分支,只有当 i == 0 时才会进入分支 | |
| case 1: | |
| f() // 当 i == 0 时函数不会被调用 | |
| } | |
| switch i { | |
| case 0: fallthrough | |
| case 1: | |
| f() // 当 i == 0 时函数也会被调用 |