Skip to content

Commit feb3465

Browse files
committed
添加1.2节的代码
1 parent 7aaa468 commit feb3465

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

1.2.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## GOPATH设置
44
go 命令依赖一个重要的环境变量:$GOPATH<sup>1</sup>
5+
6+
*(注:这个不是Go安装目录。下面以笔者的工作目录为说明,请替换自己机器上的工作目录。)*
57

68
在类似 Unix 环境大概这样设置:
79

src/1.2/main.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// 章节 1.2
2+
// $GOPATH/src/mathapp/main.go
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"mymath"
9+
)
10+
11+
func main() {
12+
fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
13+
}

src/1.2/sqrt.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// 章节 1.2
2+
// $GOPATH/src/mymath/sqrt.go
3+
package mymath
4+
5+
func Sqrt(x float64) float64 {
6+
z := 0.0
7+
for i := 0; i < 1000; i++ {
8+
z -= (z*z - x) / (2 * x)
9+
}
10+
return z
11+
}

0 commit comments

Comments
 (0)