Last active
August 29, 2015 14:18
-
-
Save porcupie/f5aefd40a220ad0b759c to your computer and use it in GitHub Desktop.
Go Tour Exercises
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
// https://tour.golang.org/methods/9 | |
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
// new error type | |
type ErrorNegativeSqrt float64 | |
func (e ErrorNegativeSqrt) Error() string { | |
return fmt.Sprint("cannot Sqrt negative number: %v", float64(e)) | |
} | |
func Sqrt(x float64) (float64, error) { | |
//return 0, nil | |
if x < 0 { | |
return 0, ErrorNegativeSqrt(x) | |
} else { | |
z := 1.0 | |
for i := 0; i < 10; i++ { | |
z = (z - (math.Pow(z, 2)-x)/(2*z)) | |
fmt.Printf("%d %f\n", i, z) | |
} | |
return z, nil | |
} | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(Sqrt(-2)) | |
} |
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
package main | |
import "fmt" | |
// https://tour.golang.org/moretypes/22 | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
// just keep two values, seed with 0, 1 | |
fibs := [2]int{0, 1} | |
//fmt.Println(fibs) | |
return func() int { | |
newval := fibs[0] + fibs[1] | |
// don't switch out until we leave | |
defer func() { | |
fibs[0], fibs[1] = fibs[1], newval | |
}() | |
// return prev last val | |
return fibs[1] | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
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
// https://tour.golang.org/flowcontrol/8 | |
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func Sqrt(x float64) float64 { | |
z := 1.0 | |
for i := 0; i < 10; i++ { | |
z = (z - (math.Pow(z, 2)-x)/(2*z)) | |
fmt.Printf("%d %f\n", i, z) | |
} | |
return z | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(math.Sqrt(2)) | |
} |
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
package main | |
// https://tour.golang.org/moretypes/19 | |
// Implement WordCount. It should return a map of the counts of each “word” in the string s. | |
import ( | |
"golang.org/x/tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
// map to store counters - use make | |
counters := make(map[string]int) | |
// split string up into words | |
words := strings.Fields(s) | |
for _, value := range words { | |
// if key doesn't exist, will have zero value | |
// increment for each word | |
counters[value] = int(counters[value] + 1) | |
} | |
return counters | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
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
// https://tour.golang.org/methods/11 | |
package main | |
import ( | |
"golang.org/x/tour/reader" | |
) | |
type MyReader struct{} | |
// TODO: Add a Read([]byte) (int, error) method to MyReader. | |
func (mr MyReader) Read(b []byte) (int, error) { | |
// simplest solution: a single 'A' - rest of b []byte is zero filled | |
//b[0] = 'A' | |
//return 1, nil | |
// second solution: fill all of b with 'A' | |
for i := 0; i < len(b); i++ { | |
b[i] = 'A' | |
} | |
return len(b), nil | |
} | |
func main() { | |
reader.Validate(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
package main | |
import ( | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func main() { | |
s := strings.NewReader("Lbh penpxrq gur pbqr!") | |
r := rot13Reader{s} | |
io.Copy(os.Stdout, &r) | |
} |
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
package main | |
// https://tour.golang.org/moretypes/14 | |
import ( | |
"fmt" | |
// "math" | |
"golang.org/x/tour/pic" | |
) | |
func Pic(dx, dy int) [][]uint8 { | |
debug := false | |
if debug { | |
fmt.Printf("dx=%d\n", dx) | |
fmt.Printf("dy=%d\n", dy) | |
} | |
// NOTE: trying to make an Array gives error: "non-constant array bound dx" | |
//var values [dx][dy]uint8 | |
// should return a _slice_ of slices ... | |
// ... of length dy | |
values := make([][]uint8, dy) | |
if debug { | |
fmt.Printf("before: %v len=%d, cap=%d\n", values, len(values), cap(values)) | |
} | |
for i := 0; i < dy; i++ { | |
// ... each element of which is a slice of dx 8-bit unsigned integers | |
inner := make([]uint8, dx) | |
values[i] = inner | |
} | |
// now fill it with something useful - some sort of function ... | |
for y := 0; y < dy; y++ { | |
for x := 0; x < dx; x++ { | |
// Interesting functions include (x+y)/2, x*y, and x^y (to compute the latter function, use math.Pow). | |
//values[y][x] = uint8((x + y) / 2) | |
values[y][x] = uint8((x * y)) | |
//values[y][x] = uint8(math.Pow(float64(x), float64(y))) | |
} | |
} | |
if debug { | |
fmt.Print("After: %v\n", values) | |
} | |
return values | |
} | |
func main() { | |
pic.Show(Pic) | |
} |
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
// https://tour.golang.org/methods/7 | |
package main | |
import "fmt" | |
type IPAddr [4]byte | |
// TODO: Add a "String() string" method to IPAddr. | |
func (addr IPAddr) String() string { | |
return fmt.Sprintf("%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]) | |
} | |
func main() { | |
addrs := map[string]IPAddr{ | |
"loopback": {127, 0, 0, 1}, | |
"googleDNS": {8, 8, 8, 8}, | |
} | |
for n, a := range addrs { | |
fmt.Printf("%v: %v\n", n, a) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment