Last active
May 28, 2018 15:46
-
-
Save rcarneva/ccd2a20223a30c67b8910c5a32eb797f to your computer and use it in GitHub Desktop.
tour of go
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 { | |
i, j := 0, 1 | |
return func() int { | |
k := i | |
i, j = j, i+j | |
return k | |
} | |
} | |
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
package main | |
import ( | |
"golang.org/x/tour/pic" | |
"image/color" | |
"image" | |
) | |
const gridsize = 200 | |
type Image struct{ | |
grid [gridsize][gridsize][3]uint8 | |
} | |
func (i Image) ColorModel () color.Model { | |
return color.RGBAModel | |
} | |
func (i Image) Bounds() image.Rectangle { | |
return image.Rect(0, 0, gridsize, gridsize) | |
} | |
func (i Image) At(x, y int) color.Color { | |
return color.RGBA{i.grid[x][y][0], i.grid[x][y][1], i.grid[x][y][2], 255} | |
} | |
func main() { | |
m := Image{} | |
for x := 0; x < gridsize; x++ { | |
for y := 0; y < gridsize; y++ { | |
m.grid[x][y][0] = (uint8)((x^y) * (x+y)) | |
m.grid[x][y][1] = (uint8)((x+y) * (x-y)) | |
m.grid[x][y][2] = (uint8)((x+y) * (x*y)) | |
} | |
} | |
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 ( | |
"golang.org/x/tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
d := make(map[string]int) | |
for _, w := range strings.Fields(s) { | |
d[w] += 1 | |
} | |
return d | |
} | |
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" | |
type IPAddr [4]byte | |
func (i IPAddr) String() string { | |
return fmt.Sprintf("%d.%d.%d.%d", i[0], i[1], i[2], i[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) | |
} | |
} |
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 ( | |
"golang.org/x/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) { | |
if t.Left != nil { | |
Walk(t.Left, ch) | |
} | |
ch <- t.Value | |
if t.Right != nil { | |
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 i:=0; i<10; i++ { | |
if <-ch1 != <-ch2 { | |
return false | |
} | |
} | |
return true | |
} | |
func main() { | |
fmt.Println(Same(tree.New(1), tree.New(2))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment