Go 语言教程实战
来源:互联网 发布:火星研究院 知乎 编辑:程序博客网 时间:2024/04/20 09:06
安装在线教程
由于墙,golang.org 国内无法访问,其在线教程也一样 tutorial.golang.org http://go-tour-zh.appspot.com/
可以在本机运行,
先安装Go 编译器 http://code.google.com/p/go/downloads/list
然后安装教程
go get code.google.com/p/go-tour/gotour
或者中文的
go get bitbucket.org/mikespook/go-tour-zh/gotour
最后执行安装产生的 gotour
执行文件,即可在http://localhost:3999 打开教程。
一些练习的答案
#46 练习:斐波纳契闭包
package mainimport "fmt"// fibonacci 函数会返回一个返回 int 的函数。func fibonacci() func() int {var a int = 1var b int = 1return func() int {c := a+ba = bb = creturn c}}func main() {f := fibonacci()for i := 0; i < 10; i++ {fmt.Println(f())}}
#69 练习:等价二叉树
package mainimport ("bitbucket.org/mikespook/go-tour-zh/tree""fmt")/*type Tree struct {Left *TreeValue intRight *Tree}*/// Walk 步进 tree t 将所有的值从 tree 发送到 channel ch。func Walk(t *tree.Tree, ch chan int) {if t != nil {Walk(t.Left, ch)ch <- t.ValueWalk(t.Right, ch)}}// Same 检测树 t1 和 t2 是否含有相同的值。func Same(t1, t2 *tree.Tree) bool {ch1 := make(chan int, 10)go Walk(t1, ch1)ch2 := make(chan int, 10)go Walk(t2, ch2)for i := 0; i < 10; i++ {if <-ch1 != <-ch2 {return false}}return true}func main() {fmt.Println(Same(tree.New(1), tree.New(1)))}
#57 练习:错误
package mainimport ("fmt")type ErrNegativeSqrt float64func (e ErrNegativeSqrt) Error() string {return fmt.Sprintf("cannot Sqrt negative number: %f", e)}func Sqrt(f float64) (float64, error) {if f < 0 {return 0, ErrNegativeSqrt(f)}x := ffor i := 0; i < 10; i++ {x = (x + f/x) / 2}return x, nil}func main() {fmt.Println(Sqrt(2))fmt.Println(Sqrt(-2))}
#44 练习:Map
package mainimport (//"fmt""strings""bitbucket.org/mikespook/go-tour-zh/wc")func WordCount(s string) map[string]int {result := map[string]int {}f := strings.Fields(s)for _, v := range f {result[v] += 1}return result}func main() {wc.Test(WordCount)}
- Go 语言教程实战
- go语言实战向导
- go语言实战向导
- Go语言实战-- 通道
- Go语言实战笔记
- Go语言简明教程
- Go语言安装教程
- Go 语言参考教程
- Go语言实战_自定义集合Set
- Go语言实战_自定义OrderedMap
- Go语言实战-- 并发示例-Runner
- Go语言实战--并发示例-Pool
- Go语言RESTful API开发实战
- go语言的cgo简单教程
- go语言的cgo简单教程
- Go实战--go语言中执行shell脚本(The way to go)
- Go实战--go语言操作sqlite数据库(The way to go)
- Go实战--go语言操作MySQL数据库(go-sql-driver/mysql)
- Nginx支持php配置
- IE9设置无法保存解决方法
- DLNA架构在机顶盒上播放云存储文件的实现
- 在头.h写函数实现体和在cpp中写实现体的区别
- c/c++-----static关键字
- Go 语言教程实战
- 现代浏览器的工作原理
- performSelectorOnMainThread: vs dispatch_async on main queue
- DirectShow 视频录制保存为avi文件
- 插入排序
- Nhibernate 过长的字符串报错 dehydration property
- java动态绑定和多态
- const用法
- Miracast