Skip to content

Instantly share code, notes, and snippets.

@jouyouyun
Created November 5, 2013 10:02
Show Gist options
  • Save jouyouyun/7316627 to your computer and use it in GitHub Desktop.
Save jouyouyun/7316627 to your computer and use it in GitHub Desktop.
Go Exercises
package main
import (
"fmt"
)
func Sqrt (x float64) float64 {
z := 0.0
for i := 0; i < 10; i++ {
z -= (z * z - x) / (2 * x)
}
return z
}
func main () {
fmt.Println (Sqrt (2))
}
package main
import (
"fmt"
"code.google.com/p/go-tour/tree"
)
/*
** Walk walks the tree t sending all values
** from the tree to the channel ch.
*/
func Walk (t *tree.Tree, ch chan int) {
if t == nil {
return
}
// left -> parent -> right
Walk (t.Left, ch)
ch <- t.Value
Walk (t.Right, ch)
}
/*
** Same determines whether the trees
** t1 and t2 contain the same values.
*/
func Same (t1, t2 *tree.Tree) bool {
ch1 := make (chan int)
ch2 := make (chan int)
go func () {
Walk (t1, ch1)
ch1 <- 0 //确保 0 是最后一个进入管道,来判断管道的值是否结束
} ()
go func () {
Walk (t2, ch2)
ch2 <- 0
} ()
for {
tmp1, tmp2 := <-ch1, <-ch2
if tmp1 == tmp2 {
fmt.Printf ("%d == %d\n", tmp1, tmp2)
if tmp1 == 0 {
return true
}
} else {
return false
}
}
}
func main () {
fmt.Println (Same (tree.New (1), tree.New (2)))
fmt.Println (Same (tree.New (1), tree.New (1)))
}
package main
import (
"strings"
"code.google.com/p/go-tour/wc"
)
func WordCount (s string) map[string]int {
str_array := strings.Fields (s)
m := make (map[string]int)
l := len (str_array)
for i := 0; i < l; i++ {
m[str_array[i]] += 1
}
return m
}
func main () {
wc.Test (WordCount)
}
package main
import "fmt"
func fibonacci () func () int {
x, y := 0, 1
return func () int {
num := x
x, y = y, x + y
return num
}
}
func main () {
f := fibonacci ()
for i := 0; i < 10; i++ {
fmt.Println (f ())
}
}
package main
import (
"fmt"
"math/cmplx"
)
func Cbrt (x complex128) complex128 {
z := complex128 (0.1)
for i := 0; i < 10; i++ {
z -= (cmplx.Pow (z, 3) - x) / (3 * cmplx.Pow (z, 2))
}
return z
}
func main () {
fmt.Println (Cbrt (2))
}
package main
import (
"fmt"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf (": cannot Sqrt negative number: %f",
float64 (e))
}
func Sqrt (x float64) (float64, error) {
if x < 0 {
return 0, ErrNegativeSqrt (x)
}
z := 0.0
for i := 0; i < 10; i++ {
z -= (z * z - x) / (2 * x)
}
return z, nil
}
func main () {
fmt.Println (Sqrt (-2))
}
package main
import (
"fmt"
"net/http"
)
type String string
func (str String) ServeHTTP (
w http.ResponseWriter, r *http.Request) {
fmt.Fprint (w, str)
}
type Struct struct {
Greeting string
Punct string
Who string
}
func (s *Struct) ServeHTTP (
w http.ResponseWriter, r *http.Request) {
fmt.Fprint (w, s.Greeting)
fmt.Fprint (w, s.Punct)
fmt.Fprint (w, s.Who)
}
func main () {
http.Handle ("/string", String ("I am a frayed kot."))
http.Handle ("/struct", &Struct {"Hello", ":", "Gophers!"})
http.ListenAndServe ("localhost:4000", nil)
}
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (r *rot13Reader) Read (ret []byte) (n int, err error) {
p := make ([]byte, 512)
l, e := r.r.Read (p)
if e != nil {
return 0, e
}
for i := 0; i < l; i++ {
tmp := p[i] + 13
if isLower (p[i]) {
if tmp > 'z' {
tmp = 'a' + tmp - 'z' - 1
}
} else if isUpper (p[i]) {
if tmp > 'Z' {
tmp = 'A' + tmp - 'Z' - 1
}
} else {
tmp -= 13
}
ret[i] = tmp
}
return l, nil
}
func isUpper (ch byte) bool {
if ch >= 'A' && ch <= 'Z' {
return true
}
return false
}
func isLower (ch byte) bool {
if ch >= 'a' && ch <= 'z' {
return true
}
return false
}
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