This commit is contained in:
Unknown
2014-10-17 23:43:37 -04:00
parent c55fd51e56
commit 7221191c30
5 changed files with 47 additions and 5 deletions

View File

@@ -9,7 +9,7 @@
## 翻译进度
6.9 [用闭包:将函数作为返回值](eBook/06.9.md)
6.10 [使用闭包调试](eBook/06.10.md)
## 支持本书

42
eBook/06.10.md Normal file
View File

@@ -0,0 +1,42 @@
# 6.10 使用闭包调试
当您在分析和调试复杂的程序时,无数个函数在不同的代码文件中相互调用,如果这时候能够准确地知道哪个文件中的具体哪个函数正在执行,对于调试是十分有帮助的。您可以使用 `runtime``log` 包中的特殊函数来实现这样的功能。包 `runtime` 中的函数 `Caller()` 提供了相应的信息,因此可以在需要的时候实现一个 `where()` 闭包函数来打印函数执行的位置:
```go
where := func() {
_, file, line, _ := runtime.Caller(1)
log.Printf("%s:%d", file, line)
}
where()
// some code
where()
// some more code
where()
```
您也可以设置 `log` 包中的 flag 参数来实现:
```go
log.SetFlags(log.Llongfile)
log.Print("")
```
或使用一个更加简短版本的 `where` 函数:
```go
var where = log.Print
func func1() {
where()
... some code
where()
... some code
where()
}
```
## 链接
- [目录](directory.md)
- 上一节:[应用闭包:将函数作为返回值](06.9.md)
- 下一节:[计算函数执行时间](06.11.md)

3
eBook/06.11.md Normal file
View File

@@ -0,0 +1,3 @@
# 6.11 计算函数执行时间
154

View File

@@ -1,4 +0,0 @@
# 6.10 使用闭包调试
153

View File

@@ -56,6 +56,7 @@
- 6.8 [闭包](06.8.md)
- 6.9 [应用闭包:将函数作为返回值](06.9.md)
- 6.10 [使用闭包调试](06.10.md)
- 6.11 [计算函数执行时间](06.11.md)
- 第7章数组array与切片slice
- 第8章Maps
- 第9章package