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
| 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
| //下面的代码展示了另一种在调试时使用 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 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
| // 在需要的时候实现一个 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
| // 应用闭包:将函数作为返回值 | |
| // 工厂函数 | |
| 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
| start := time.Now() | |
| longCalculation() | |
| end := time.Now() | |
| delta := end.Sub(start) | |
| fmt.Printf("longCalculation took this amount of time: %s\n", delta) |
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" | |
| "time" | |
| ) | |
| const LIM = 41 | |
| var fibs [LIM]uint64 |
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() { | |
| s1 := []byte{'p', 'o', 'e', 'm'} //且 | |
| s2 := s1[2:] | |
| // s2[1] = 't' // 这个时候, s1, s2 都会改变 | |
| for i := 0; i < len(s1); i++ { | |
| fmt.Printf("Slice at %d is %v\n", i, s1[i]) |
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() { | |
| sl_from := []int{1, 2, 3} | |
| sl3 := []int{1, 2, 3} | |
| sl3 = append(sl3, 4, 5, 6) | |
| sl3 = append(sl3, sl_from...) | |
| fmt.Println(sl3) |