Created
April 7, 2012 10:20
-
-
Save minikomi/2327219 to your computer and use it in GitHub Desktop.
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
/* Exercise 1 */ | |
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func Sqrt(x float64) float64 { | |
z := float64(1) | |
for i:= 0; i < 10; i++ { | |
z = z - (math.Pow(z, 2) - x)/(2 * z) | |
} | |
return z | |
} | |
func main() { | |
fmt.Println(Sqrt(2) - math.Sqrt(2)) | |
} | |
/* exercise 2 */ | |
package main | |
import ( | |
"code.google.com/p/go-tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
f := strings.Fields(s) | |
words := make(map[string]int) | |
for _, word := range f { | |
words[word] += 1 | |
} | |
return words | |
} | |
func main() { | |
wc.Test(WordCount) | |
} | |
/* Exercise 3 */ | |
package main | |
import "code.google.com/p/go-tour/pic" | |
func Pic(dx, dy int) [][]uint8 { | |
res := make([][]uint8, dy) | |
for y := 0; y < dy; y++ { | |
res[y] = make([]uint8, dx) | |
for x := range res[y] { | |
res[y][x] = uint8(x^y) | |
} | |
} | |
return res | |
} | |
func main() { | |
pic.Show(Pic) | |
} | |
/* Exercise 4 */ | |
package main | |
import "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
last, i := 0, 1 | |
return func() int { | |
last, i = i, i + last | |
return i | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} | |
/* Exercise 5 */ | |
package main | |
import ( | |
"fmt" | |
"math/cmplx" | |
) | |
func Cbrt(x complex128) complex128 { | |
z := complex128(x) | |
for i := 0; i < 10; i++ { | |
z = z - ((cmplx.Pow(z, 3)-x)/(3 * cmplx.Pow(z, 2))) | |
} | |
return z | |
} | |
func main() { | |
fmt.Println(Cbrt(2)) | |
} | |
/* Exercise 6 */ | |
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
type ErrNegativeSqrt float64 | |
func (e ErrNegativeSqrt) Error() string { | |
return := fmt.Sprintf("cannot Sqrt negative number: %v", float64(e)) | |
} | |
func Sqrt(f float64) (float64, error) { | |
if f >= 0 { | |
z := float64(1) | |
for i := 0; i < 10; i++ { | |
z = z - (math.Pow(z, 2)-f)/(2*z) | |
} | |
return z, nil | |
} | |
return 0, ErrNegativeSqrt(f) | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(Sqrt(-2)) | |
} | |
/#59 rot13/ | |
package main | |
import ( | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (rot *rot13Reader) Read(p []byte) (n int, err error) { | |
n, err = rot.r.Read(p) | |
if err != nil { | |
return n, err | |
} | |
lower := "abcdefghijklmnopqrstuvwxyz" | |
upper := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
for i, x := range p { | |
y := string(x) | |
if strings.Contains(lower, y) { | |
p[i] = lower[(strings.Index(lower, y)+13)%26] | |
} else if strings.Contains(upper, y) { | |
p[i] = upper[(strings.Index(upper, y)+13)%26] | |
} | |
} | |
return | |
} | |
func main() { | |
s := strings.NewReader( | |
"grfg") | |
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