Last active
December 10, 2015 21:08
-
-
Save authorNari/4492840 to your computer and use it in GitHub Desktop.
http://go-tour-jp.appspot.com/ のチュートリアル
This file contains hidden or 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) (z float64) { | |
z = 1.0 | |
prez := float64(0) | |
for ; math.Abs(z-prez) > 0.001; { | |
prez = z | |
z = z - ((z*z-x)/2) | |
} | |
return | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(math.Sqrt(2)) | |
} |
This file contains hidden or 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 ( | |
"tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
res := make(map[string]int) | |
for _, i := range strings.Fields(s) { | |
res[i]++ | |
} | |
return res | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
This file contains hidden or 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 "tour/pic" | |
func Pic(dx, dy int) (res [][]uint8) { | |
res = make([][]uint8, dx) | |
for x := 0; x < dx; x++ { | |
res[x] = make([]uint8, dy) | |
for y := 0; y < dy; y++ { | |
res[x][y] = uint8(x^y) | |
} | |
} | |
return res | |
} | |
func main() { | |
pic.Show(Pic) | |
} |
This file contains hidden or 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 { | |
n := 0 | |
fib := func() int { | |
var f func(int) int; | |
f = func(z int) int { | |
if z < 2 { | |
return z | |
} | |
return f(z - 2) + f(z - 1) | |
} | |
i := f(n) | |
n++ | |
return i | |
} | |
return fib | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
This file contains hidden or 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 ErrNegativeSqrt float64; | |
func (e ErrNegativeSqrt) Error() string { | |
return fmt.Sprintf("cannot Sqrt negative number: %f", float64(e)) | |
} | |
func Sqrt(f float64) (float64, error) { | |
if f < 0 { | |
return 0, ErrNegativeSqrt(f) | |
} | |
return 0, nil | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(Sqrt(-2)) | |
} |
This file contains hidden or 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 ( | |
"tour/tree" | |
"fmt" | |
"reflect" | |
) | |
func walkleaf(t *tree.Tree, ch chan int) { | |
if t.Left != nil { | |
walkleaf(t.Left, ch) | |
} | |
ch <- t.Value | |
if t.Right != nil { | |
walkleaf(t.Right, ch) | |
} | |
} | |
// Walk walks the tree t sending all values | |
// from the tree to the channel ch. | |
func Walk(t *tree.Tree, ch chan int) { | |
walkleaf(t, ch) | |
close(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) | |
quite1 := make(chan []int) | |
quite2 := make(chan []int) | |
go Walk(t1, ch1) | |
go Walk(t2, ch2) | |
get := func(ch chan int, quite chan []int) { | |
ts := make([]int, 0) | |
for { | |
s, ok := <- ch | |
if ok { | |
ts = append(ts, s) | |
} else { | |
quite <- ts | |
} | |
} | |
} | |
go get(ch1, quite1) | |
go get(ch2, quite2) | |
ts1 := <- quite1 | |
ts2 := <- quite2 | |
return reflect.DeepEqual(ts1, ts2) | |
} | |
func main() { | |
fmt.Printf("%t\n", Same(tree.New(1), tree.New(1))) | |
fmt.Printf("%t\n", Same(tree.New(1), tree.New(2))) | |
} |
This file contains hidden or 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, ch chan fetchResult) | |
} | |
var fetched = make(map[string]bool) | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
func Crawl(url string, depth int, fetcher Fetcher) { | |
quite := make(chan fetchResult) | |
if fetched[url] { | |
return | |
} | |
// This implementation doesn't do either: | |
if depth <= 0 { | |
return | |
} | |
go fetcher.Fetch(url, quite) | |
res := <- quite | |
if res.err != nil { | |
fmt.Println(res.err) | |
return | |
} | |
fetched[url] = true | |
fmt.Printf("found: %s %q\n", url, res.response.body) | |
for _, u := range res.response.urls { | |
Crawl(u, depth-1, fetcher) | |
} | |
return | |
} | |
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 | |
} | |
type fetchResult struct { | |
response *fakeResult | |
err error | |
} | |
func (f *fakeFetcher) Fetch(url string, ch chan fetchResult) { | |
if res, ok := (*f)[url]; ok { | |
ch <- fetchResult{res, nil} | |
return | |
} | |
ch <- fetchResult{&fakeResult{"", nil}, nil} | |
} | |
// 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/a", | |
"http://golang.org/pkg/b", | |
}, | |
}, | |
"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