Golang

fmt.Println(“Let’s Go!”)

Table of Contents

  1. 练习:Loop & Function
  2. 练习:Slice
  3. 练习:Map
  4. 练习:Fibonacci Closure
  5. 练习:Stringer
  6. 练习:Error
  7. 练习:Reader
  8. 练习:rot13Reader

练习:Loop & Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := float64(1)
// for i := 0; i < 10; i++ {
// z -= (z*z-x)/(2*z)
// }
for {
y := z - (z * z - x) / (2 * z)
if math.Abs(y - z) < 1e-10 {
return y
}
z = y
}
return z
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(math.Sqrt(2))
}

练习:Slice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
ans := make([][]uint8,dy)
for i := 0; i < dy; i++ {
ans[i] = make([]uint8,dx)
for j := 0; j < dx; j++ {
ans[i][j] = uint8(i ^ j)
}
}
return ans
}
func main() {
pic.Show(Pic)
}

练习:Map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
m := make(map[string]int)
for _,v := range strings.Fields(s) {
m[v]++
}
return m
}
func main() {
wc.Test(WordCount)
}

练习:Fibonacci Closure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main
import "fmt"
// fibonacci 函数会返回一个返回 int 的函数。
func fibonacci() func() int {
num1 := 0
num2 := 1
return func() int {
ans := num1
num1 = num2
num2 = ans + num1
return ans
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}

练习:Stringer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package main
import "fmt"
type IPAddr [4]byte
func (addr IPAddr) String() string {
return fmt.Sprintf("%v.%v.%v.%v", addr[0], addr[1], addr[2], addr[3])
}
func main() {
addrs := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for n, a := range addrs {
fmt.Printf("%v: %v\n", n, a)
}
}

练习:Error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main
import (
"fmt"
"math"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e))
}
func ErrorSqrt(x float64) (float64, error) {
if x < 0 {
return 0, ErrNegativeSqrt(x)
}
z := float64(1)
for {
y := z - (z * z - x) / (2 * z)
if math.Abs(y - z) < 1e-10 {
return y, nil
}
z = y
}
return z, nil
}
func main() {
fmt.Println(ErrorSqrt(2))
fmt.Println(ErrorSqrt(-2))
}

练习:Reader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main
import (
"golang.org/x/tour/reader"
"strings"
)
type MyReader struct{}
func (MyReader) Read(b []byte) (int, error) {
r := strings.NewReader("A")
n, err := r.Read(b)
return n, err
}
func main() {
reader.Validate(MyReader{})
}

练习:rot13Reader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (self rot13Reader) Read(b []byte) (int, error) {
length, err := self.r.Read(b)
for i := range b {
switch {
case b[i] >= 'a' && b[i] < 'n':
fallthrough
case b[i] >= 'A' && b[i] < 'N':
b[i] += 13
case b[i] >= 'n' && b[i] <= 'z':
fallthrough
case b[i] >= 'N' && b[i] <= 'Z':
b[i] -= 13
}
}
return length, err
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}