2013-01-19から1日間の記事一覧
A Tour of Go chapter 59 Exercise: Rot13 Reader 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 gzipped data) and retu…
A Tour of Go chapter 58 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, imple…
A Tour of Go chapter 57 Exercise: HTTP Handlers Implement the following types and define ServeHTTP methods on them. Register them to handle specific paths in your web server. type String string type Struct struct { Greeting string Punct st…
A Tour of Go chapter 56 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 number…
A Tour of Go chapter 46 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: z = z - (z ** 3 - x) / (z *…
A Tour of Go chapter 45 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. Example package main import "fmt" // fibo…
A Tour of Go chapter 44 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 integer…
A Tour of Go chapter 43 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 …
A Tour of Go capter 42 Exercise: Loops and Functions As a simple way to play with functions and loops, implement the square root function using Newton's method. In this case, Newton's method is to approximate Sqrt(x) by picking a starting …