Documentation ¶
Overview ¶
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 call to fmt.Sprint(e) inside the Error method will send the program into an infinite loop. You can avoid this by converting e first: fmt.Sprint(float64(e)). Why?
Change your Sqrt function to return an ErrNegativeSqrt value when given a negative number.
NOTE: 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
TODO: Exercise: Images (https://go.dev/tour/methods/25) Instructions: > 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.
See: https://go.dev/pkg/image/#Image for "necessary methods"
> `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.RGBA{v, v, 255, 255}` in this one.
Exercise: Readers
Implement an `io.Reader` type that emits an ASCII character 'A'
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).
TODO: 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. The rot13Reader type is provided for you. Make it an io.Reader by implementing its Read method. rot13 cipher: https://en.wikipedia.org/wiki/ROT13
NOTE: Exercise Instructions 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.) (Use uint8(intValue) to convert between types.)
Exercise: Stringers
Make the type IPAddr implement fmt.Stringer to print the address as a dotted quad ¶
For instance, IPAddr{1, 2, 3, 4} should print as "1.2.3.4"
Index ¶
- func ColorImage()
- func Fibonacci()
- func IPAddrStringerTest()
- func MathPic()
- func Pic(dx, dy int) [][]uint8
- func ReaderValidate()
- func Rot13ReaderTest()
- func Same(t1, t2 *tree.Tree) bool
- func Sqrt(x float64) (float64, error)
- func SqrtErr()
- func TreeMain()
- func Walk(t *tree.Tree, ch chan int)
- func WordCount(s string) map[string]int
- func WordCountTest()
- type ErrNegativeSqrt
- type IPAddr
- type Image
- type MyReader
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ColorImage ¶
func ColorImage()
func IPAddrStringerTest ¶
func IPAddrStringerTest()
func ReaderValidate ¶
func ReaderValidate()
func Rot13ReaderTest ¶
func Rot13ReaderTest()
func WordCountTest ¶
func WordCountTest()
Types ¶
type ErrNegativeSqrt ¶
type ErrNegativeSqrt float64
func (ErrNegativeSqrt) Error ¶
func (e ErrNegativeSqrt) Error() string