Last active
August 29, 2015 14:01
-
-
Save gom/6d93c5153e729eb405db to your computer and use it in GitHub Desktop.
A tour of Go Exercise http://go-tour-jp.appspot.com/
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
foobar |
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" | |
"math" | |
) | |
func Sqrt(x float64) float64 { | |
z := 1.0 | |
prev := 0.0 | |
for { | |
z = z - (z * z - x) / (2 * z) | |
if math.Abs(prev - z) < 1e-15 { | |
break | |
} | |
prev = z | |
} | |
return z | |
} | |
func main() { | |
x := 2.0 | |
fmt.Println(Sqrt(x)) | |
fmt.Println(math.Sqrt(x)) | |
} |
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 "code.google.com/p/go-tour/pic" | |
func Pic(dx, dy int) [][]uint8 { | |
matrix := make([][]uint8, dy) | |
for y := range matrix { | |
row := make([]uint8, dx) | |
for x := range row { | |
row[x] = uint8(x^y) | |
} | |
matrix[y] = row | |
} | |
return matrix | |
} | |
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
package main | |
import ( | |
"code.google.com/p/go-tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
counts := make(map[string]int) | |
for _, word := range strings.Fields(s) { | |
counts[word] += 1 | |
} | |
return counts | |
} | |
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
package main | |
import "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
x1, x2 := 0, 1 | |
return func() int { | |
x1, x2 = x2, x1 + x2 | |
return x2 | |
} | |
} | |
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
import ( | |
"fmt" | |
"math" | |
"math/cmplx" | |
) | |
func Cbrt(x complex128) complex128 { | |
z := complex128(2) | |
s := complex128(0) | |
for { | |
z = z - (cmplx.Pow(z, 3) - x) / (3 * (z * z)) | |
if cmplx.Abs(s - z) < 1e-17 { | |
break | |
} | |
s = z | |
} | |
return z | |
} | |
func main() { | |
r := Cbrt(2) | |
fmt.Println(r) | |
fmt.Println(cmplx.Pow(r, 3)) | |
fmt.Println(math.Cbrt(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" | |
"net/http" | |
) | |
type String string | |
type Struct struct { | |
Greeting string | |
Punct string | |
Who string | |
} | |
func (s String) ServeHTTP( | |
w http.ResponseWriter, | |
r *http.Request) { | |
fmt.Fprint(w, s) | |
} | |
func (s *Struct) ServeHTTP( | |
w http.ResponseWriter, | |
r *http.Request) { | |
fmt.Fprint(w, s) | |
} | |
func main() { | |
// your http.Handle calls here | |
http.Handle("/string", String("I'm a frayed knot.")) | |
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"}) | |
http.ListenAndServe("localhost:4000", nil) | |
} |
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 ( | |
"code.google.com/p/go-tour/pic" | |
"image" | |
"image/color" | |
) | |
type Image struct{ | |
w, h int | |
color uint8 | |
} | |
func (m *Image) ColorModel() color.Model { | |
return color.RGBAModel | |
} | |
func (m *Image) Bounds() image.Rectangle { | |
return image.Rect(0, 0, m.w, m.h) | |
} | |
func (m *Image) At(x, y int) color.Color { | |
return color.RGBA { m.color * 2 + uint8(x), m.color * 100 + uint8(y), 255, 255 } | |
} | |
func main() { | |
m := Image{255, 255, 128} | |
pic.ShowImage(&m) | |
} |
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 (rot *rot13Reader) Read(p []byte) (n int, err error) { | |
n, err = rot.r.Read(p) | |
in_range := func(b, min, max byte) bool { | |
return (b >= min && b <= max) | |
} | |
for i := 0; i < len(p); i++ { | |
switch { | |
case in_range(p[i], 'A', 'M') || in_range(p[i], 'a', 'm'): | |
p[i] += 13 | |
case in_range(p[i], 'N', 'Z') || in_range(p[i], 'n', 'z'): | |
p[i] -= 13 | |
} | |
} | |
return | |
} | |
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 | |
import ( | |
"code.google.com/p/go-tour/tree" | |
"fmt" | |
) | |
// Walk walks the tree t sending all values | |
// from the tree to the channel ch. | |
func Walk(t *tree.Tree, ch chan int) { | |
Walk_(t, ch) | |
close(ch) | |
} | |
func Walk_ (t *tree.Tree, ch chan int) { | |
if t != nil { | |
Walk_(t.Left, ch) | |
ch <- t.Value | |
Walk_(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 v := range ch1 { | |
if v != <-ch2 { return false } | |
} | |
return true | |
} | |
func main() { | |
ch := make(chan int) | |
go Walk(tree.New(1), ch) | |
for i := range ch { | |
fmt.Println(i) | |
} | |
fmt.Println(Same(tree.New(1), tree.New(1))) | |
fmt.Println(Same(tree.New(1), tree.New(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" | |
) | |
type Fetcher interface { | |
// Fetch returns the body of URL and | |
// a slice of URLs found on that page. | |
Fetch(url string) (body string, urls []string, err error) | |
} | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
func Crawl(url string, depth int, fetcher Fetcher) { | |
next := make(chan []string) | |
results := make(map[string]bool) | |
go crawl_(url, fetcher, next) | |
results[url] = true | |
for depth > 0 { | |
next_urls := <- next | |
for _, u := range next_urls { | |
if _, done := results[u]; !done { | |
results[u] = true | |
go crawl_(u, fetcher, next) | |
} | |
} | |
depth-- | |
} | |
return | |
} | |
func crawl_ (url string, fetcher Fetcher, next chan []string) { | |
body, urls, err := fetcher.Fetch(url) | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
fmt.Printf("found: %s %q\n", url, body) | |
} | |
next <- urls | |
} | |
type results map[string]string | |
func main() { | |
Crawl("http://golang.org/", 4, fetcher) | |
} | |
// fakeFetcher is Fetcher that returns canned results. | |
type fakeFetcher map[string]*fakeResult | |
type fakeResult struct { | |
body string | |
urls []string | |
} | |
func (f fakeFetcher) Fetch(url string) (string, []string, error) { | |
if res, ok := f[url]; ok { | |
return res.body, res.urls, nil | |
} | |
return "", nil, fmt.Errorf("not found: %s", url) | |
} | |
// fetcher is a populated fakeFetcher. | |
var fetcher = fakeFetcher{ | |
"http://golang.org/": &fakeResult{ | |
"The Go Programming Language", | |
[]string{ | |
"http://golang.org/pkg/", | |
"http://golang.org/cmd/", | |
}, | |
}, | |
"http://golang.org/pkg/": &fakeResult{ | |
"Packages", | |
[]string{ | |
"http://golang.org/", | |
"http://golang.org/cmd/", | |
"http://golang.org/pkg/fmt/", | |
"http://golang.org/pkg/os/", | |
}, | |
}, | |
"http://golang.org/pkg/fmt/": &fakeResult{ | |
"Package fmt", | |
[]string{ | |
"http://golang.org/", | |
"http://golang.org/pkg/", | |
}, | |
}, | |
"http://golang.org/pkg/os/": &fakeResult{ | |
"Package os", | |
[]string{ | |
"http://golang.org/", | |
"http://golang.org/pkg/", | |
}, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment