exercise.tour.go google的go官方教程答案
/* Exercise: Loops and Functions #43 */
package main import (
"fmt"
"math"
) func Sqrt(x float64) float64 {
z := float64(.)
s := float64()
for {
z = z - (z*z - x)/(*z)
if math.Abs(s-z) < 1e- {
break
}
s = z
}
return s
} func main() {
fmt.Println(Sqrt())
fmt.Println(math.Sqrt())
} /******************************************************************************************************/ /* Exercise: Maps #44 */
package main import (
"tour/wc"
"strings"
) func WordCount(s string) map[string]int {
ss := strings.Fields(s)
num := len(ss)
ret := make(map[string]int)
for i := ; i < num; i++ {
(ret[ss[i]])++
}
return ret
} func main() {
wc.Test(WordCount)
} /******************************************************************************************************/ /* Exercise: Slices #45 */
package main import "tour/pic" func Pic(dx, dy int) [][]uint8 {
ret := make([][]uint8, dy)
for i := ; i < dy; i++ {
ret[i] = make([]uint8, dx)
for j := ; j < dx; j++ {
ret[i][j] = uint8(i^j+(i+j)/)
}
}
return ret
} func main() {
pic.Show(Pic)
} /******************************************************************************************************/ /* Exercise: Fibonacci closure #46 */
package main import "fmt" // fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
x :=
y :=
return func() int {
x,y = y,x+y
return x
}
} func main() {
f := fibonacci()
for i := ; i < ; i++ {
fmt.Println(f())
}
} /******************************************************************************************************/ /* Advanced Exercise: Complex cube roots #47 */
package main import (
"fmt"
"math/cmplx"
) func Cbrt(x complex128) complex128 {
z := complex128()
s := complex128()
for {
z = z - (cmplx.Pow(z,) - x)/( * (z * z))
if cmplx.Abs(s-z) < 1e- {
break
}
s = z
}
return z
} func main() {
fmt.Println(Cbrt())
} /******************************************************************************************************/ /* Exercise: Errors #57 */
package main import (
"fmt"
) type ErrNegativeSqrt float64 func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negativ number: %g", float64(e))
} func Sqrt(f float64) (float64, error) {
if f < {
return , ErrNegativeSqrt(f)
}
return , nil
} func main() {
fmt.Println(Sqrt())
fmt.Println(Sqrt(-))
} /******************************************************************************************************/ /* Exercise: Images #58 */
package main import (
"image"
"tour/pic"
"image/color"
) type Image struct{
Width, Height int
colr uint8
} func (r *Image) Bounds() image.Rectangle {
return image.Rect(, , r.Width, r.Height)
} func (r *Image) ColorModel() color.Model {
return color.RGBAModel
} func (r *Image) At(x, y int) color.Color {
return color.RGBA{r.colr+uint8(x), r.colr+uint8(y), , }
} func main() {
m := Image{, , }
pic.ShowImage(&m)
} /******************************************************************************************************/
/* Exercise: Rot13 Reader #59: 'You cracked the code!' */
package main import (
"io"
"os"
"strings"
) type rot13Reader struct {
r io.Reader
} func (rot *rot13Reader) Read(p []byte) (n int, err error) {
n,err = rot.r.Read(p)
for i := ; i < len(p); i++ {
if (p[i] >= 'A' && p[i] < 'N') || (p[i] >='a' && p[i] < 'n') {
p[i] +=
} else if (p[i] > 'M' && p[i] <= 'Z') || (p[i] > 'm' && p[i] <= 'z'){
p[i] -=
}
}
return
} func main() {
s := strings.NewReader(
"Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
} /******************************************************************************************************/ /* Exercise: Equivalent Binary Trees #67 */
package main import (
"tour/tree"
"fmt"
) // Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
_walk(t, ch)
close(ch) } func _walk(t *tree.Tree, ch chan int) {
if t != nil {
_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 Walk(t1, ch1)
go Walk(t2, ch2)
for i := range ch1 {
if i != <- ch2 {
return false
}
}
return true
} func main() {
//tree.New(2)
ch := make(chan int)
go Walk(tree.New(), ch)
for v := range ch {
fmt.Print(v)
}
fmt.Println(Same(tree.New(), tree.New()))
fmt.Println(Same(tree.New(), tree.New()))
} /******************************************************************************************************/ /* Exercise: Web Crawler #69 */
package main import (
"fmt"
) 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)
} var store map[string]bool func Krawl(url string, fetcher Fetcher, Urls chan []string) {
body, urls, err := fetcher.Fetch(url)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("found: %s %q\n", url, body)
}
Urls <- urls
} func Crawl(url string, depth int, fetcher Fetcher) {
Urls := make(chan []string)
go Krawl(url, fetcher, Urls)
band :=
store[url] = true // init for level 0 done
for i := ; i < depth; i++ {
for band > {
band--
next := <- Urls
for _, url := range next {
if _, done := store[url] ; !done {
store[url] = true
band++
go Krawl(url, fetcher, Urls)
}
}
}
}
return
} func main() {
store = make(map[string]bool)
Crawl("http://golang.org/", , fetcher)
} // fakeFetcher is Fetcher that returns canned results.
type fakeFetcher map[string]*fakeResult type 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/",
},
},
}
exercise.tour.go google的go官方教程答案的更多相关文章
- Google reCAPTCHA 2 : Protect your site from spam and abuse & Google reCAPTCHA 2官方教程
1
- [转]Google Guava官方教程(中文版)
Google Guava官方教程(中文版) http://ifeve.com/google-guava/
- Google Guava官方教程(中文版)
Google Guava官方教程(中文版) 原文链接 译文链接 译者: 沈义扬,罗立树,何一昕,武祖 校对:方腾飞 引言 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库, ...
- Google Guava官方教程(中文版)地址
Google Guava官方教程(中文版) http://ifeve.com/google-guava/ 瓜娃啊瓜娃
- ubuntu16.04安装tensorflow官方教程与机器学习资料【学习笔记】
tensorflow官网有官方的安装教程:https://www.tensorflow.org/install/install_linux google的机器学习官方快速入门教程:https://de ...
- Python-第三方库requests详解(附requests中文官方教程)
转自http://blog.csdn.net/cyjs1988/article/details/73294774 Python+requests中文官方教程: http://www.python-re ...
- Ceisum官方教程2 -- 项目实例(workshop)
原文地址:https://cesiumjs.org/tutorials/Cesium-Workshop/ 概述 我们很高兴欢迎你加入Cesium社区!为了让你能基于Cesium开发自己的3d 地图项目 ...
- Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译
本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
- Unity性能优化(4)-官方教程Optimizing graphics rendering in Unity games翻译
本文是Unity官方教程,性能优化系列的第四篇<Optimizing graphics rendering in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
随机推荐
- 垃圾收集器GC的种类
堆内存的结构:
- HashMap源代码深入剖析
..
- POJ2632——Crashing Robots
Crashing Robots DescriptionIn a modernized warehouse, robots are used to fetch the goods. Careful pl ...
- libevent安装
libevent : 名气最大,应用最广泛,历史悠久的跨平台事件库:libev : 较libevent而言,设计更简练,性能更好,但对Windows支持不够好:libuv : 开发node的过程中需要 ...
- 蓝牙(2)用BluetoothAdapter搜索蓝牙设备示例
注意在搜索之前要先打开蓝牙设备 package com.e.search.bluetooth.device; import java.util.Set; import android.app.Acti ...
- Hibernate数据库持久层框架
Hibernate是一种Java语言下的对象关系映射解决方案. 它是使用GNU宽通用公共许可证发行的自由.开源的软件.它为面向对象的领域模型到传统的关系型数据库的映射,提供了一个使用方便的框架.Hib ...
- 基于注解的SpringMVC整合JPA
转载位置:http://www.blogjava.net/sxyx2008/archive/2010/11/02/336768.html 实体类 Department package com.sj.b ...
- [原]Unity3D深入浅出 - 认识开发环境中的GameObject菜单栏
Create Empty:创建空对象 Create Other:创建其他对象 Particle System:创建粒子系统 Camera:创建相机 GUI Text:GUI文本 GUI Texture ...
- ASP.NET的六种验证控件的使用
C# 中的验证控件分为一下六种 :1 CompareValidator:比较验证,两个字段的值是否相等,比如判断用户输入的密码和确认密码是否一致,则可以用改控件: 2 CustomValidator ...
- webapp开发经验和资料
开发经验: 开发资料: 1. http://xuui.net/librarys/webapps/webapp-development-of-commonly-used-code-snippets.ht ...