Skip to content

Instantly share code, notes, and snippets.

@schnabear
Last active September 2, 2019 08:24
Show Gist options
  • Save schnabear/7de89f44ea5608c23c61924c125af145 to your computer and use it in GitHub Desktop.
Save schnabear/7de89f44ea5608c23c61924c125af145 to your computer and use it in GitHub Desktop.
Go Tour Exercise
package main
import (
"fmt"
"golang.org/x/tour/tree"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
recurseWalk(t, ch)
close(ch)
}
func recurseWalk(t *tree.Tree, ch chan int) {
if t.Left != nil {
recurseWalk(t.Left, ch)
}
ch <- t.Value
if t.Right != nil {
recurseWalk(t.Right, ch)
}
}
// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
ch1 := make(chan int)
ch2 := make(chan int)
go Walk(t1, ch1)
go Walk(t2, ch2)
for i := range ch1 {
if i != <-ch2 {
return false
}
}
return true
}
func main() {
ch := make(chan int)
go Walk(tree.New(1), ch)
for i := 0; i < 10; i++ {
fmt.Println(<-ch)
}
fmt.Println(Same(tree.New(1), tree.New(1)))
fmt.Println(Same(tree.New(1), tree.New(2)))
}
package main
import (
"fmt"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e))
}
func Sqrt(x float64) (float64, error) {
if x < 0 {
return 0, ErrNegativeSqrt(x)
}
var v float64
z := float64(1)
for float32(v) != float32(z) {
v = z
z -= (z*z - x) / (2 * z)
}
return z, nil
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-2))
}
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
x, y, z := 0, 0, 1
return func() int {
x = y
y = z
z = x + y
return x
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
package main
import (
"image"
"image/color"
"golang.org/x/tour/pic"
)
type Image struct {
w int
h int
}
func (i Image) Bounds() image.Rectangle {
return image.Rect(0, 0, i.w, i.h)
}
func (i Image) ColorModel() color.Model {
return color.RGBAModel
}
func (i Image) At(x, y int) color.Color {
v := uint8((x + y) / 2)
return color.RGBA{v, v, 255, 255}
}
func main() {
m := Image{255, 255}
pic.ShowImage(m)
}
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
var v float64
z := float64(1)
for float32(v) != float32(z) {
v = z
z -= (z*z - x) / (2 * z)
}
return z
}
func main() {
fmt.Println(Sqrt(16))
}
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
count := make(map[string]int)
for _, word := range words {
count[word]++
}
return count
}
func main() {
wc.Test(WordCount)
}
package main
import "golang.org/x/tour/reader"
type MyReader struct{}
func (MyReader) Read(b []byte) (int, error) {
b[0] = 'A'
return 1, nil
}
func main() {
reader.Validate(MyReader{})
}
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (rot rot13Reader) Read(b []byte) (int, error) {
n, err := rot.r.Read(b)
for i := range b {
switch {
case 'A' <= b[i] && 'M' >= b[i]:
fallthrough
case 'a' <= b[i] && 'm' >= b[i]:
b[i] += 13
case 'N' <= b[i] && 'Z' >= b[i]:
fallthrough
case 'n' <= b[i] && 'z' >= b[i]:
b[i] -= 13
}
}
return n, err
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
data := make([][]uint8, dy)
for y := 0; y < dy; y++ {
data[y] = make([]uint8, dx)
for x := 0; x < dx; x++ {
data[y][x] = uint8((x + y) / 2)
}
}
return data
}
func main() {
pic.Show(Pic)
}
package main
import "fmt"
type IPAddr [4]byte
func (ip IPAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
}
func main() {
hosts := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for name, ip := range hosts {
fmt.Printf("%v: %v\n", name, ip)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment