Go练习(A Tour of Go)答案

Sqrt
用牛顿法实现开方函数。

package main

import (
    "fmt"
    "math"
)

func Sqrt(x float64) float64 {
    z := 1.0
    z_last := 1.0
    z_limit := 0.0000001
    for {
        z = (z+x/z)/2
        if math.Abs(z_last - z) < z_limit {
            break
        }
        z_last = z
    }
    return z
}

func main() {
    fmt.Println(Sqrt(5)==math.Sqrt(5))
}

Map
实现 `WordCount`。它应当返回一个含有 s 中每个 “词” 个数的 map。函数 wc.Test 针对这个函数执行一个测试用例,并打印成功或者失败。

package main

import (
    "code.google.com/p/go-tour/wc"
    "strings"
)

func WordCount(s string) map[string]int {
    m := make(map[string]int)
    words := strings.Fields(s)
    var word string
    
    for _,word = range(words) {
        m[word]++
    }
    
    return m
}

func main() {
    wc.Test(WordCount)
}

Fibonacci
实现一个 fibonacci 函数,返回一个函数(一个闭包)可以返回连续的斐波纳契数。

package main

import "fmt"

// 使用数组
func fibonacci() func() int {
    n := 0
    last := []int{1,1}
    return func () int {
        n++
        i := n%2
        if n > 2 {
            last[i] = last[0]+last[1]
        }
        
        return last[i]
    }
}
// 使用a/b两个变量
func fibonacci2() func() int {
    a, b := 0, 1
    return func () int {
        a = a+b
        b = a-b
        return a
    }
}

func main() {
    f := fibonacci()
    for i := 0; i < 10; i++ {
        fmt.Println(f())
    }
}

错误
从之前的练习中复制 Sqrt 函数,并修改使其返回 error 值。

package main

import (
    "fmt"
    "math"
)

type ErrNegativeSqrt float64

func (e ErrNegativeSqrt) Error() string {
	return fmt.Sprintf("cantnot Sqrt negative number: %v", float64(e))
}

func Sqrt(x float64) (float64, error) {
    if x < 0 {
        err := ErrNegativeSqrt(x)
        return 0, err
    }
    
    return math.Sqrt(x), nil
}

func main() {
    fmt.Println(Sqrt(2))
    fmt.Println(Sqrt(-2))
}

HTTP 处理
实现下面的类型,并在其上定义 ServeHTTP 方法。在 web 服务器中注册它们来处理指定的路径。

package main

import (
    "fmt"
    "net/http"
)

type Hello struct{}

type String string

type Struct struct {
    Greeting string
    Punct    string
    Who      string
}

func (s String) ServeHTTP(
    w http.ResponseWriter,
    r *http.Request) {
	response := fmt.Sprintf("%v", s)
    fmt.Fprint(w, response)
}

func (s Struct) ServeHTTP(
    w http.ResponseWriter,
    r *http.Request) {
	response := fmt.Sprintf("%v%v%v", s.Greeting,s.Punct,s.Who)
    fmt.Fprint(w, response)
}



func main() {
    http.Handle("/string", String("I'm a frayed knot."))
    http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
    http.ListenAndServe("localhost:4000", nil)
}

21 评论

  1. Pingback: nike air max 90
  2. Pingback: tiffany and co outlet
  3. Pingback: Hermes Online Outlet
  4. Pingback: north face outlet
  5. Pingback: north face outlet
  6. Pingback: black adidas shoes
  7. Pingback: cheap air jordans
  8. Pingback: camisetas de futbol
  9. Pingback: cheap stone island
  10. Pingback: cheap stone island
  11. Pingback: pandora outlet
  12. Pingback: cheap pandora charms
  13. Pingback: cheap retro jordans
  14. Pingback: nike roshe run
  15. Pingback: cheap air jordans

发表评论