博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
go-tour练习解答
阅读量:6925 次
发布时间:2019-06-27

本文共 7133 字,大约阅读时间需要 23 分钟。

go-tour对于想学习golang的人来说是一个很好的教程。

首先go-tour是有,但是需要FQ。如果不能FQ的,也可以在google code上自己下载go-tour源码,go build,run一下,在自己的机器上就可以跑这个教程了。

提醒下,如果是后者,这篇文章中的程序对应的import包就需要进行相应修改了。

下面给出我的go-tour中的Exercise的解答(在web上已经测试通过)

第一个循环10次:

package mainimport (     "fmt")func Sqrt(x float64) float64 {     z := float64(1)     for i := 0; i <= 10; i++ {          z = z - (z*z - x) / (2 * z)     }     return z}func main() {     fmt.Println(Sqrt(2))}

第二次无限接近:

package mainimport (     "fmt"     "math")func Sqrt(x float64) float64 {     z := float64(1)     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))}

package mainimport (     "tour/wc"     "strings")func WordCount(s string) map[string]int {     ret := make(map[string]int)     arr := strings.Fields(s)     for _, val := range arr {          ret[val]++     }     return ret}func main() {     wc.Test(WordCount)}

package mainimport "tour/pic"func Pic(dx, dy int) [][]uint8 {     ret := make([][]uint8, dy)     for i:=0; i

package mainimport "fmt"// fibonacci is a function that returns// a function that returns an int.func fibonacci() func() int {     sum1 := 0     sum2 := 1     return func() int{          out := sum1 + sum2          sum1 = sum2          sum2 = out          return out              }}func main() {     f := fibonacci()     for i := 0; i < 10; i++ {          fmt.Println(f())     }}

package mainimport (     "fmt"     "math/cmplx"     )func Cbrt(x complex128) complex128 {     z := complex128(1)     for {          if y := z-(cmplx.Pow(z,3) - x)/(3 * z * z); cmplx.Abs(y - z) < 1e-10 {               return y          } else {               z = y          }     }     return z}func main() {     fmt.Println(Cbrt(2))}

package mainimport (     "fmt"     "math")type ErrNegativeSqrt float64func (e ErrNegativeSqrt) Error() string {     return "cannot Sqrt negative number:" + fmt.Sprint(float64(e))}func Sqrt(f float64) (float64, error) {     if f < 0 {          return 0, ErrNegativeSqrt(f)     }         z := float64(1)     for {          y := z - (z*z-f)/(2*z)          if math.Abs(y-z) < 1e-10 {               return y, nil          }          z = y     }     return z, nil}func main() {     fmt.Println(Sqrt(2))     fmt.Println(Sqrt(-2))}

package mainimport (     "image"     "tour/pic"     "image/color")type Image struct{     W int     H int}func(self Image) Bounds() image.Rectangle {     return image.Rect(0, 0, self.W, self.H)}func(self Image) ColorModel() color.Model {     return color.RGBAModel}func(self Image) At(x,y int) color.Color {     return color.RGBA{uint8(x), uint8(y), 255, 255}}func main() {     m := Image{W:100, H:100}     pic.ShowImage(m)}

package mainimport (     "io"     "os"     "strings")type rot13Reader struct {     r io.Reader}func(self rot13Reader)Read(p []byte) (n int, err error){     self.r.Read(p)     leng := len(p)     for i := 0; i < leng; i++ {          switch{          case p[i] >= 'a' && p[i] < 'n':               fallthrough          case p[i] >= 'A' && p[i] < 'N':               p[i] = p[i] + 13          case p[i] >= 'n' && p[i] <= 'z':               fallthrough          case p[i] >= 'N' && p[i] <= 'Z':               p[i] = p[i] - 13          }     }     return leng, nil}func main() {     s := strings.NewReader(          "Lbh penpxrq gur pbqr!")     r := rot13Reader{s}     io.Copy(os.Stdout, &r)}

package mainimport (     "tour/tree"     )// Walk walks the tree t sending all values// from the tree to the channel ch.func Walk(t *tree.Tree, ch chan int) {     if t == nil {          return     }         Walk(t.Left, ch)     ch <- t.Value     Walk(t.Right, ch)}// Same determines whether the trees// t1 and t2 contain the same values.func Same(t1, t2 *tree.Tree) bool {     ch1 := make(chan int)     ch2 := make(chan int)     go func() {          Walk(t1, ch1)          ch1 <- 0     }()         go func() {          Walk(t2, ch2)          ch2 <- 0     }()         for {          t1 := <-ch1          t2 := <-ch2          if t1 == 0 && t2 == 0 {               return true;          }                   if t1 == t2 {               continue;          } else {               return false;          }     }     return true}func main() {     ch := make(chan int)     go func() {          Walk(tree.New(1), ch)          ch <- 0     }()         for {          t := <-ch          if t == 0 {               break;          }          println(t)     }         println(Same(tree.New(1), tree.New(2)))}

package mainimport (     "fmt"     "sync")type Fetcher interface {     // Fetch returns the body of URL and     // a slice of URLs found on that page.     Fetch(url string) (body string, urls []string, err error)}// Crawl uses fetcher to recursively crawl// pages starting with url, to a maximum of depth.func Crawl(url string, depth int, fetcher Fetcher, out chan string, end chan bool) {     if depth <= 0 {          end <- true          return     }     if _, ok := crawled[url]; ok {          end <- true          return     }     crawledMutex.Lock()     crawled[url] = true     crawledMutex.Unlock()     body, urls, err := fetcher.Fetch(url)     if err != nil {          out <- fmt.Sprintln(err)          end <- true          return     }     out <- fmt.Sprintf("found: %s %q\n", url, body)     subEnd := make(chan bool)     for _, u := range urls {          go Crawl(u, depth-1, fetcher, out, subEnd)     }     for i := 0; i < len(urls); i++ {          <- subEnd     }     end <- true}var crawled = make(map[string]bool)var crawledMutex sync.Mutexfunc main() {     out := make(chan string)     end := make(chan bool)     go Crawl("http://golang.org/", 4, fetcher, out, end)     for {          select {          case t := <- out:               fmt.Print(t)          case <- end:               return          }     }}// fakeFetcher is Fetcher that returns canned results.type fakeFetcher map[string]*fakeResulttype fakeResult struct {     body string     urls     []string}func (f *fakeFetcher) Fetch(url string) (string, []string, error) {     if res, ok := (*f)[url]; ok {          return res.body, res.urls, nil     }     return "", nil, fmt.Errorf("not found: %s", url)}// fetcher is a populated fakeFetcher.var fetcher = &fakeFetcher{     "http://golang.org/": &fakeResult{          "The Go Programming Language",          []string{               "http://golang.org/pkg/",               "http://golang.org/cmd/",          },     },     "http://golang.org/pkg/": &fakeResult{          "Packages",          []string{               "http://golang.org/",               "http://golang.org/cmd/",               "http://golang.org/pkg/fmt/",               "http://golang.org/pkg/os/",          },     },     "http://golang.org/pkg/fmt/": &fakeResult{          "Package fmt",          []string{               "http://golang.org/",               "http://golang.org/pkg/",          },     },     "http://golang.org/pkg/os/": &fakeResult{          "Package os",          []string{               "http://golang.org/",               "http://golang.org/pkg/",          },     },}

转载地址:http://dgjjl.baihongyu.com/

你可能感兴趣的文章
QQ一键退朝效果
查看>>
man手册的基本使用
查看>>
ios9---通讯录权限
查看>>
我的友情链接
查看>>
oracle锁表查询及解锁
查看>>
ngnix之日志管理
查看>>
[置顶] Jquery乱码终极解决方案
查看>>
子网掩码、ip地址、主机号、网络号、网络地址、广播地址
查看>>
SplashScreen
查看>>
前端框架前景和后续发展预测
查看>>
UIPickerView 滚动选择器
查看>>
rac数据库RMAN 备份与恢复
查看>>
交换性钾可助大米减少放射污染
查看>>
云计算之分布式表格系统
查看>>
Apache Commons工具集简介
查看>>
Java socket教程
查看>>
maven中依赖的scope各个取值的含义
查看>>
IPv6协议解析
查看>>
linux文件时间戳的意义和区别:atime, ctime, mtime
查看>>
ubuntu jdk
查看>>