Last active
August 29, 2015 14:20
-
-
Save krmaxwell/d94f609e00843fedcafb to your computer and use it in GitHub Desktop.
Tour of Go exercises
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
// https://tour.golang.org/moretypes/22 | |
package main | |
import "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
n1, n2 := 1,1 | |
return func() int { | |
n1, n2 = n2, n1+n2 | |
return n2 | |
} | |
} | |
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 ( | |
"fmt" | |
"math" | |
) | |
func Sqrt(x float64) float64 { | |
z1, z2 := 1.0, 1.0 | |
maxerr := 0.0001 | |
for { | |
z1 = z1 - (z1*z1-x)/(2*z1) | |
if math.Abs(z2-z1) < maxerr { | |
return z1 | |
} | |
z2 = z1 | |
} | |
} | |
func main() { | |
n := 2.0 | |
fmt.Println(Sqrt(n), math.Sqrt(n), math.Abs(Sqrt(n)-math.Sqrt(n))) | |
} |
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
// https://tour.golang.org/moretypes/19 | |
package main | |
import ( | |
"strings" | |
"golang.org/x/tour/wc" | |
) | |
func WordCount(s string) map[string]int { | |
words := strings.Fields(s) | |
count := make(map[string]int) | |
for _, word := range words { | |
count[word]++ | |
} | |
return count | |
} | |
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
// https://tour.golang.org/moretypes/14 | |
package main | |
import "golang.org/x/tour/pic" | |
func Pic(dx, dy int) [][]uint8 { | |
img := make([][]uint8, dy) | |
for i := 0 ; i < dy ; i++ { | |
img[i] = make([]uint8, dx) | |
for j := 0 ; j < dx ; j++ { | |
img[i][j] = uint8(i*j) | |
} | |
} | |
return img | |
} | |
func main() { | |
pic.Show(Pic) | |
} |
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
// https://tour.golang.org/methods/7 | |
package main | |
import "fmt" | |
type IPAddr [4]byte | |
// TODO: Add a "String() string" method to IPAddr. | |
func (ip IPAddr) String() string { | |
return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]) | |
} | |
func main() { | |
addrs := map[string]IPAddr{ | |
"loopback": {127, 0, 0, 1}, | |
"googleDNS": {8, 8, 8, 8}, | |
} | |
for n, a := range addrs { | |
fmt.Printf("%v: %v\n", n, a) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment