Last active
August 29, 2015 14:08
-
-
Save shiwork/0287e107d20d0e2822a3 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 ( | |
| "code.google.com/p/go-tour/wc" | |
| "strings" | |
| ) | |
| func WordCount(s string) map[string]int { | |
| wc := make(map[string]int) | |
| for _, ss := range strings.Fields(s) { | |
| wc[ss]++ | |
| } | |
| return wc | |
| } | |
| 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 { | |
| fi, fj := 0, 1 | |
| return func() int { | |
| fi, fj = fj, fi+fj | |
| return fj | |
| } | |
| } | |
| 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" | |
| ) | |
| func Cbrt(x complex128) complex128 { | |
| z := complex128(1) | |
| for i:=0; i<100000; i++{ | |
| z = z-(z*z*z-x)/(3*z*z) | |
| } | |
| return z | |
| } | |
| func main() { | |
| fmt.Println(Cbrt(2)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment