Last active
August 27, 2019 15:48
-
-
Save anonymouss/2930b2412f36c08d8936a5fb06ed4b88 to your computer and use it in GitHub Desktop.
a tour of go
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" | |
"golang.org/x/tour/tree" | |
) | |
const LEN = 10 | |
func Walk(t *tree.Tree, ch chan int) { | |
if t == nil { | |
return | |
} | |
Walk(t.Left, ch) | |
ch <- t.Value | |
Walk(t.Right, ch) | |
} | |
func Same(t1, t2 *tree.Tree) bool { | |
ch1 := make(chan int, LEN) | |
ch2 := make(chan int, LEN) | |
go Walk(t1, ch1) | |
go Walk(t2, ch2) | |
for i := 0; i < LEN; i++ { | |
if <-ch1 != <-ch2 { | |
return false | |
} | |
} | |
return true | |
} | |
func main() { | |
fmt.Println(Same(tree.New(1), tree.New(1))) | |
fmt.Println(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" | |
"sync" | |
) | |
type Fetcher interface { | |
Fecth(url string) (body string, urls []string, err error) | |
} | |
func Crawl(url string, depth int, fetcher Fetcher, urlMap *SafeUrlMap, wg *sync.WaitGroup) { | |
defer wg.Done() | |
if depth <= 0 || urlMap.isVisited(url) { | |
return | |
} | |
urlMap.setVisited(url) | |
body, urls, err := fetcher.Fecth(url) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Printf("found: %s %q\n", url, body) | |
for _, u := range urls { | |
wg.Add(1) | |
go Crawl(u, depth-1, fetcher, urlMap, wg) | |
} | |
return | |
} | |
type SafeUrlMap struct { | |
urlMap map[string]bool | |
mtx sync.Mutex | |
} | |
func (m *SafeUrlMap) isVisited(url string) bool { | |
m.mtx.Lock() | |
defer m.mtx.Unlock() | |
return m.urlMap[url] | |
} | |
func (m *SafeUrlMap) setVisited(url string) { | |
m.mtx.Lock() | |
m.urlMap[url] = true | |
m.mtx.Unlock() | |
} | |
func main() { | |
urlMap := SafeUrlMap{urlMap: make(map[string]bool)} | |
var wg sync.WaitGroup | |
wg.Add(1) | |
go Crawl("https://golang.org/", 4, fetcher, &urlMap, &wg) | |
wg.Wait() | |
} | |
type fakeFetcher map[string]*fakeResult | |
type fakeResult struct { | |
body string | |
urls []string | |
} | |
func (f fakeFetcher) Fecth(url string) (string, []string, error) { | |
if res, ok := f[url]; ok { | |
return res.body, res.urls, nil | |
} | |
return "", nil, fmt.Errorf("not fount: %s", url) | |
} | |
var fetcher = fakeFetcher{ | |
"https://golang.org/": &fakeResult{ | |
"The Go Programming Language", | |
[]string{ | |
"https://golang.org/pkg/", | |
"https://golang.org/cmd/", | |
}, | |
}, | |
"https://golang.org/pkg/": &fakeResult{ | |
"Packages", | |
[]string{ | |
"https://golang.org/", | |
"https://golang.org/cmd/", | |
"https://golang.org/pkg/fmt/", | |
"https://golang.org/pkg/os/", | |
}, | |
}, | |
"https://golang.org/pkg/fmt/": &fakeResult{ | |
"Package fmt", | |
[]string{ | |
"https://golang.org/", | |
"https://golang.org/pkg/", | |
}, | |
}, | |
"https://golang.org/pkg/os/": &fakeResult{ | |
"Package os", | |
[]string{ | |
"https://golang.org/", | |
"https://golang.org/pkg/", | |
}, | |
}, | |
} |
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: %v", float64(e)) | |
} | |
func sqrt(x float64) (float64, error) { | |
if x < 0 { | |
return x, ErrNegativeSqrt(x) | |
} | |
const EPSILON = 0.00000001 | |
z := 1.0 | |
abs := func(x float64) float64 { | |
if x < 0.0 { | |
return -x | |
} | |
return x | |
} | |
for { | |
zp := z * z | |
if abs(zp-x) < EPSILON { | |
return z, nil | |
} | |
z -= (zp - x) / (2 * z) | |
} | |
} | |
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 "fmt" | |
func fibonacci() func() int { | |
n2 := 0 | |
n1 := 0 | |
return func() int { | |
fib := n1 + n2 | |
if fib == 0 { | |
fib = 1 | |
} | |
n2 = n1 | |
n1 = fib | |
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 ( | |
"image" | |
"image/color" | |
"golang.org/x/tour/pic" | |
) | |
type Image struct { | |
width, height int | |
color uint8 | |
} | |
func (img Image) ColorModel() color.Model { | |
return color.RGBAModel | |
} | |
func (img Image) Bounds() image.Rectangle { | |
return image.Rect(0, 0, img.width, img.height) | |
} | |
func (img Image) At(x, y int) color.Color { | |
return color.RGBA{img.color + uint8(x), img.color + uint8(y), 255, 255} | |
} | |
func main() { | |
m := Image{256, 256, 0} | |
pic.ShowImage(m) | |
} |
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 ( | |
"strings" | |
"golang.org/x/tour/wc" | |
) | |
func wordCount(s string) map[string]int { | |
m := make(map[string]int) | |
words := strings.Fields(s) | |
for _, w := range words { | |
if _, ok := m[w]; ok { | |
m[w]++ | |
} else { | |
m[w] = 1 | |
} | |
} | |
return m | |
} | |
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 ( | |
"golang.org/x/tour/reader" | |
) | |
type MyReader struct{} | |
func (r MyReader) Read(b []byte) (n int, err error) { | |
b[0] = 'A' | |
return 1, nil | |
} | |
func main() { | |
reader.Validate(MyReader{}) | |
} |
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 ( | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (rot rot13Reader) Read(b []byte) (n int, err error) { | |
if n, err = rot.r.Read(b); err != nil { | |
return | |
} | |
length := len(b) | |
for i := 0; i < length; i++ { | |
if (b[i] >= 'A' && b[i] < 'N') || (b[i] >= 'a' && b[i] < 'n') { | |
b[i] += 13 | |
} else if (b[i] > 'M' && b[i] <= 'Z') || (b[i] > 'm' && b[i] <= 'z') { | |
b[i] -= 13 | |
} | |
} | |
return | |
} | |
func main() { | |
s := strings.NewReader("Lbh penpxrq gur pbqr!") | |
r := rot13Reader{s} | |
io.Copy(os.Stdout, &r) | |
} |
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 ( | |
"math" | |
"golang.org/x/tour/pic" | |
) | |
// (x+y)/2, x*y, x^y, x*log(y) or x%(y+1)。 | |
func getPic(dx, dy int) [][]uint8 { | |
img := make([][]uint8, dy) | |
for y := range img { | |
img[y] = make([]uint8, dx) | |
for x := range img[y] { | |
px := float64(x) * math.Log(float64(y)) | |
img[y][x] = uint8(px) | |
} | |
} | |
return img | |
} | |
func main() { | |
// Show() func has const dx, dy = 256 | |
pic.Show(getPic) | |
// https://codebeautify.org/base64-to-image-converter | |
} |
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" | |
func sqrt(x float64) float64 { | |
const EPSILON = 0.00000001 | |
z := 1.0 | |
abs := func(x float64) float64 { | |
if x < 0.0 { | |
return -x | |
} | |
return x | |
} | |
for { | |
zp := z * z | |
if abs(zp-x) < EPSILON { | |
return z | |
} | |
z -= (zp - x) / (2 * z) | |
} | |
} | |
func main() { | |
fmt.Println(sqrt(2), (sqrt(5)-1)/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 IPAddr [4]byte | |
func (addr IPAddr) String() string { | |
return fmt.Sprintf("\"%v.%v.%v.%v\"", addr[0], addr[1], addr[2], addr[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