添加1.2节的代码

This commit is contained in:
gelosie
2012-09-20 14:59:35 +08:00
parent 7aaa468d93
commit feb3465acc
3 changed files with 26 additions and 0 deletions

2
1.2.md
View File

@@ -2,6 +2,8 @@
## GOPATH设置
go 命令依赖一个重要的环境变量:$GOPATH<sup>1</sup>
*这个不是Go安装目录。下面以笔者的工作目录为说明请替换自己机器上的工作目录。*
在类似 Unix 环境大概这样设置:

13
src/1.2/main.go Normal file
View File

@@ -0,0 +1,13 @@
// 章节 1.2
// $GOPATH/src/mathapp/main.go
package main
import (
"fmt"
"mymath"
)
func main() {
fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
}

11
src/1.2/sqrt.go Normal file
View File

@@ -0,0 +1,11 @@
// 章节 1.2
// $GOPATH/src/mymath/sqrt.go
package mymath
func Sqrt(x float64) float64 {
z := 0.0
for i := 0; i < 1000; i++ {
z -= (z*z - x) / (2 * x)
}
return z
}