[关闭]
@qiezhian 2014-11-26T16:14:21.000000Z 字数 3029 阅读 1391

Go语言基础

Go


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, (x+y)/2, and x*y.

(You need to use a loop to allocate each []uint8 inside the [][]uint8.)

(Use uint8(intValue) to convert between types.)

  1. package main
  2. import "code.google.com/p/go-tour/pic"
  3. func Pic(dx, dy int) [][]uint8 {
  4. result := make([][] uint8,dy)
  5. for i := range result {
  6. result[i] = make([] uint8, dx)
  7. }
  8. for i := 0; i<dy; i++ {
  9. for j:=0; j<dx; j++ {
  10. result[i][j] = uint8(i+j)
  11. }
  12. }
  13. return result
  14. }
  15. func main() {
  16. pic.Show(Pic)
  17. }

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.

  1. package main
  2. import (
  3. "code.google.com/p/go-tour/wc"
  4. )
  5. func WordCount(s string) map[string]int {
  6. m := make( map[string]int )
  7. length := len(s)
  8. var word string
  9. for i,j := 0,0; j<length; j++ {
  10. if s[j] == ' ' {
  11. word = s[i:j]
  12. i = j+1
  13. m[word]++
  14. } else if j==length-1 {
  15. word = s[i:j+1]
  16. m[word]++
  17. }
  18. }
  19. return m
  20. //return map[string]int{"x": 1}
  21. }
  22. func main() {
  23. wc.Test(WordCount)
  24. }

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.

  1. package main
  2. import "fmt"
  3. // fibonacci is a function that returns
  4. // a function that returns an int.
  5. func fibonacci() func() int {
  6. a := 0
  7. b := 1
  8. return func() int {
  9. tmp := a + b
  10. a = b
  11. b = tmp
  12. return a
  13. }
  14. }
  15. func main() {
  16. f := fibonacci()
  17. for i := 0; i < 10; i++ {
  18. fmt.Println(f())
  19. }
  20. }

Advanced Exercise: Complex cube roots

Let's explore Go's built-in support for complex numbers via the complex64 and complex128 types. For cube roots, Newton's method amounts to repeating:

Find the cube root of 2, just to make sure the algorithm works. There is a Pow function in the math/cmplx package.

  1. package main
  2. import ( "fmt";"math/cmplx" )
  3. func Cbrt(x complex128) complex128 {
  4. z := x
  5. y := z - ( cmplx.Pow( z, 3 ) - x)/(3*cmplx.Pow( z, 2 ) )
  6. for i := 0; i<100; i++ {
  7. z = y
  8. y = z - ( cmplx.Pow( z, 3 ) - x)/(3*cmplx.Pow( z, 2 ) )
  9. }
  10. return y
  11. }
  12. func main() {
  13. fmt.Println(Cbrt(2))
  14. }

Exercise: Errors

Copy your Sqrt function from the earlier exercises 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

  1. type ErrNegativeSqrt float64

and make it an error by giving it a

  1. func (e ErrNegativeSqrt) Error() string

method such that ErrNegativeSqrt(-2).Error() returns "cannot Sqrt negative number: -2".

Note: a call to fmt.Print(e) inside the Error method will send the program into an infinite loop. You can avoid this by converting e first: fmt.Print(float64(e)). Why?

Change your Sqrt function to return an ErrNegativeSqrt value when given a negative number.

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type ErrNegativeSqrt float64
  7. func (e ErrNegativeSqrt ) Error() string {
  8. return fmt.Sprintf( "cannot Sqrt negative number: %f",
  9. float64(e) )
  10. }
  11. func Sqrt(f float64) (float64, error) {
  12. var y float64 = 0
  13. var e error = nil
  14. if f < 0 {
  15. e = ErrNegativeSqrt( f )
  16. } else {
  17. z := f
  18. y = z - ( math.Pow( z, 2 ) - f)/(2*z)
  19. for i := 0; i<100; i++ {
  20. z = y
  21. y = z - ( math.Pow( z, 2 ) - f)/(2*z)
  22. }
  23. }
  24. return y, e
  25. }
  26. func main() {
  27. fmt.Println(Sqrt(2))
  28. fmt.Println(Sqrt(-2))
  29. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注