Last active
December 3, 2017 16:42
-
-
Save soichisumi/641aa1b2e8462c7e56b0ed3ffc02cce0 to your computer and use it in GitHub Desktop.
Go sources for go tour (https://go-tour-jp.appspot.com/welcome/1)
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
title |
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/pic" | |
"math" | |
) | |
func Pic(dx, dy int) [][]uint8 { | |
s := make([][]uint8, dx) | |
for i := 0; i < dx; i++ { | |
s[i] = make([]uint8, dy) | |
} | |
for i := 0; i < dx; i++ { | |
for j := 0; j < dy; j++ { | |
s[i][j] = uint8((i * j) % math.MaxUint8) | |
} | |
} | |
return s | |
} | |
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 ( | |
"golang.org/x/tour/pic" | |
"math" | |
) | |
func Pic(dx, dy int) [][]uint8 { | |
s := make([][]uint8, dx) | |
for i := range s { | |
s[i] = make([]uint8, dy) | |
} | |
for i:= range s { | |
for j := range s[i]{ | |
s[i][j] = uint8((i * j) % math.MaxUint8) | |
} | |
} | |
return s | |
} | |
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 ( | |
"golang.org/x/tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
words := strings.Fields(s) | |
counter := map[string]int{} | |
for _, w := range words { | |
counter[w]++ | |
} | |
return counter | |
} | |
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 "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { //内部の関数のスコープをここで切っている | |
n := 0 | |
a, b, tmp := 0, 1, 0 | |
return func() int{ | |
switch(n){ | |
case 0: | |
n++ | |
return 0 | |
case 1: | |
n++ | |
return 1 | |
} | |
tmp = a+b | |
a=b | |
b=tmp | |
return b | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment