Last active
August 29, 2015 13:57
-
-
Save auycro/9498922 to your computer and use it in GitHub Desktop.
A 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
//A Tour of Go #38 | |
//Exercise: Slices | |
package main | |
import "code.google.com/p/go-tour/pic" | |
func Pic(dx, dy int) [][]uint8 { | |
f:= make([][]uint8,dy) | |
for y:=0;y<dy;y++{ | |
f[y]= make([]uint8,dx) | |
for x:=0;x<dx;x++{ | |
f[y][x]= uint8(y*x) | |
} | |
} | |
return f | |
} | |
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
//A Tour of Go #43 | |
//Exercise: Maps | |
package main | |
import ( | |
"code.google.com/p/go-tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
arr := strings.Fields(s) | |
word := make(map[string] int) | |
for i:=0;i<len(arr);i++{ | |
word[arr[i]]=word[arr[i]]+1 | |
} | |
return word | |
} | |
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
//A Tour of Go #46 | |
//Exercise: Fibonacci closure | |
package main | |
import "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func(int) int { | |
var v [100]int | |
return func(x int) int{ | |
v[x]=x | |
if x>=2 { | |
v[x]=v[x-1]+v[x-2] | |
} | |
return v[x] | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f(i)) | |
} | |
} |
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
//A Tour of Go #50 | |
//Advanced Exercise: Complex cube roots | |
package main | |
import ( | |
"fmt" | |
//"math/cmplx" | |
) | |
func Cbrt(x complex128) complex128 { | |
z := complex128(1) | |
for i:=0;i<10;i++{ | |
sum := ((z*z*z)-x)/(3*z*z) | |
//sum := (cmplx.Pow(z,3)-x)/(3*z*z) | |
z = z - sum | |
} | |
return z | |
} | |
func main() { | |
fmt.Println(Cbrt(2)) | |
} |
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
//A Tour of Go #60 | |
//Exercise: HTTP Handlers | |
package main | |
import ( | |
"net/http" | |
"fmt" | |
) | |
type String string | |
type Struct struct { | |
Greeting string | |
Punct string | |
Who string | |
} | |
func (s String) ServeHTTP(w http.ResponseWriter,r *http.Request) { | |
fmt.Fprint(w, "Hello!,"+s) | |
} | |
func (s *Struct) ServeHTTP(w http.ResponseWriter,r *http.Request) { | |
fmt.Fprint(w, "Hello!,"+s.Who) | |
} | |
func main() { | |
http.Handle("/string", String("I'm a frayed knot.")) | |
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"}) | |
http.ListenAndServe("localhost:4000", nil) | |
} |
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
//A Tour of Go #62 | |
//Exercise: Images | |
package main | |
import ( | |
"fmt" | |
"code.google.com/p/go-tour/pic" | |
"image" | |
"image/color" | |
) | |
type Image struct{ | |
V,W,H int | |
} | |
func (r Image) ColorModel() color.Model{ | |
return color.RGBAModel | |
} | |
func (r Image) Bounds() image.Rectangle{ | |
return image.Rect(0, 0, r.W, r.H) | |
} | |
func (r Image) At(x, y int) color.Color{ | |
return color.RGBA{uint8(x), uint8(y), uint8(r.V), uint8(r.V)} | |
} | |
func main() { | |
m := Image{80,80,80} | |
fmt.Println(m.ColorModel()) | |
fmt.Println(m.Bounds()) | |
fmt.Println(m.At(0, 0).RGBA()) | |
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
//A Tour of Go #63 | |
//Exercise: Rot13 Reader | |
package main | |
import ( | |
"io" | |
"os" | |
"strings" | |
//"fmt" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func Rot13(b byte) byte{ | |
//Rot13 Algorithm here | |
return b | |
} | |
func (r13 *rot13Reader) Read(p []byte) (int, error) { | |
n,err := r13.r.Read(p) | |
for i := range(p[:n]) { | |
p[i] = Rot13(p[i]) | |
} | |
return n,err | |
} | |
func main() { | |
s := strings.NewReader( | |
"Lbh penpxrq gur pbqr!") | |
r := rot13Reader{s} | |
io.Copy(os.Stdout, &r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment