go-tour

前言

关于 Go 语言之旅 的练习题

Equivalent Binary Trees

/* Exercise: Equivalent Binary Trees 1. Implement the Walk function. 2. Test the Walk function. The function tree.New(k) constructs a randomly-structured (but always sorted) binary tree holding the values k, 2k, 3k, ..., 10k. Create a new channel ch and kick off the walker: go Walk(tree.New(1), ch) Then read and print 10 values from the channel. It should be the numbers 1, 2, 3, ..., 10. 3. Implement the Same function using Walk to determine whether t1 and t2 store the same values.

Errors

/* Exercise: Errors Copy your Sqrt function from the earlier exercise and modify it to return an error value. Sqrt should return a non-nil error value when given a negative number, as it doesn't support complex numbers. Create a new type type ErrNegativeSqrt float64 and make it an error by giving it a func (e ErrNegativeSqrt) Error() string method such that ErrNegativeSqrt(-2).Error() returns "cannot Sqrt negative number: -2". Note: A

Fibonacci closure

/* Exercise: Fibonacci closure Let's have some fun with functions. Implement a fibonacci function that returns a function (a closure) that returns successive fibonacci numbers (0, 1, 1, 2, 3, 5, ...). */ package main import "fmt" func fibonacci() func() int { before, val := 0, 1 return func() int { ret := before before, val = val, before + val return ret } } func main() { f := fibonacci() for i := 0; i < 10; i++ { fmt.

Images

/* Exercise: Images Remember the picture generator you wrote earlier? Let's write another one, but this time it will return an implementation of image.Image instead of a slice of data. Define your own Image type, implement the necessary methods, and call pic.ShowImage. Bounds should return a image.Rectangle, like image.Rect(0, 0, w, h). ColorModel should return color.RGBAModel. At should return a color; the value v in the last picture generator corresponds to color.

Loops and Functions

```go /* Exercise: Loops and Functions As a way to play with functions and loops, let’s implement a square root function: given a number x, we want to find the number z for which z² is most nearly x. Computers typically compute the square root of x using a loop. Starting with some guess z, we can adjust z based on how close z² is to x, producing a better guess:

Maps

```go /* Exercise: Maps Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Test function runs a test suite against the provided function and prints success or failure. You might find strings.Fields helpful. */ package main import ( “strings” "golang.org/x/tour/wc" ) func wordCount(s string) map[string]int { m := make(map[string]int) var ss []string ss = strings.Fields(s) for i := 0; i < len(ss); i++ { m[ss[i]]++ }

Readers

```go /* Exercise: Readers Implement a Reader type that emits an infinite stream of the ASCII character ‘A’. */ package main import “golang.org/x/tour/reader” type myReader struct{} func (r myReader) Read(c []byte) (int, error) { i := 0 for ; i < len©; i++ { c[i] = ‘A’ } for ; i < cap©; i++ { c = append(c, ‘A’) } return cap(c), nil } func main() { reader.Validate(myReader{}) }

rot13Reader

```go /* Exercise: rot13Reader A common pattern is an io.Reader that wraps another io.Reader, modifying the stream in some way. For example, the gzip.NewReader function takes an io.Reader (a stream of compressed data) and returns a *gzip.Reader that also implements io.Reader (a stream of the decompressed data). Implement a rot13Reader that implements io.Reader and reads from an io.Reader, modifying the stream by applying the rot13 substitution cipher to all alphabetical characters.

Slices

/* Exercise: Slices Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values. The choice of image is up to you. Interesting functions include (x+y)/2, x*y, and x^y. (You need to use a loop to allocate each []uint8 inside the [][]uint8.