This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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{} | |
// TODO: Add a Read([]byte) (int, error) method to MyReader. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make the IPAddr type 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". | |
package main | |
import ( | |
"fmt" | |
) | |
type IPAddr [4]byte |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" | |
// fibonacci is a function that returns | |
// a function that returns an int. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.) | |
package main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See https://gist.github.com/bilsalak/a53cddbee61647eea9de552193cfcedf for part 2 | |
// | |
// Try other initial guesses for z, like x, or x/2. How close are your function's | |
// results to the math.Sqrt in the standard library? | |
package main | |
import ( | |
"fmt" | |
"math" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See https://gist.github.com/bilsalak/a6016b2e564418402b1431491040f372 for part 1 | |
// | |
// Next, change the loop condition to stop once the value has stopped changing | |
// (or only changes by a very small amount). See if that's more or fewer than 10 iterations. | |
package main | |
import ( | |
"fmt" | |
"math" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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: | |
// | |
// z -= (z*z - x) / (2*z) | |
// Repeating this adjustment makes the guess better and better until we reach an answer that is as close | |
// to the actual square root as can be. | |
// |