Collapsing tree into more sane Go project layout

since I was in here looking at something unrelated...
This commit is contained in:
Dan Buch
2012-12-13 18:12:43 -05:00
parent 270eb1fc2d
commit 44999623da
19 changed files with 23 additions and 31 deletions

View File

@@ -0,0 +1,29 @@
package main
import (
"fmt"
"time"
)
func main() {
c := make(chan int, 2)
q := make(chan bool)
go func(c chan int) {
for i := range c {
fmt.Println("\t", i, "<- popped")
time.Sleep(300 * time.Millisecond)
}
}(c)
go func(c chan int) {
i := 0
for {
c <- i
fmt.Println("pushed ->", i)
i += 1
}
}(c)
<-q
}

View File

@@ -0,0 +1,31 @@
package main
import (
"fmt"
"math"
"time"
)
func sum(a []int, c chan int) {
sum := 0
for _, v := range a {
sum += v
}
to_sleep := time.Duration(100*math.Abs(float64(sum))) * time.Millisecond
fmt.Printf("Sleeping %s...\n", to_sleep)
time.Sleep(to_sleep)
c <- sum
}
func main() {
a := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(a[:len(a)/2], c)
go sum(a[len(a)/2:], c)
x := <-c
fmt.Printf("x = %d\n", x)
y := <-c
fmt.Printf("y = %d\n", y)
fmt.Println(x, y, x+y)
}

View File

@@ -0,0 +1,23 @@
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
prev := 0
cur := 1
return func() int {
old_cur := cur
cur = prev + cur
prev = old_cur
return cur
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}

View File

@@ -0,0 +1,32 @@
package main
import (
"fmt"
"net/http"
)
type String string
type Struct struct {
Greeting string
Punct string
Who string
}
func (s String) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprintf(w, "%s\n", s)
}
func (s Struct) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprintf(w, "%s%s %s", s.Greeting, s.Punct, s.Who)
}
func main() {
http.Handle("/string", String("I'm a frayed knot."))
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
http.ListenAndServe("localhost:4000", nil)
}

View File

@@ -0,0 +1,19 @@
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
counts := make(map[string]int)
words := strings.Fields(s)
for _, word := range words {
counts[word]++
}
return counts
}
func main() {
wc.Test(WordCount)
}

View File

@@ -0,0 +1,97 @@
package main
import (
"fmt"
"io"
"os"
"strings"
)
var lookupTable = map[byte]byte{
'A': 'N',
'B': 'O',
'C': 'P',
'D': 'Q',
'E': 'R',
'F': 'S',
'G': 'T',
'H': 'U',
'I': 'V',
'J': 'W',
'K': 'X',
'L': 'Y',
'M': 'Z',
'N': 'A',
'O': 'B',
'P': 'C',
'Q': 'D',
'R': 'E',
'S': 'F',
'T': 'G',
'U': 'H',
'V': 'I',
'W': 'J',
'X': 'K',
'Y': 'L',
'Z': 'M',
'a': 'n',
'b': 'o',
'c': 'p',
'd': 'q',
'e': 'r',
'f': 's',
'g': 't',
'h': 'u',
'i': 'v',
'j': 'w',
'k': 'x',
'l': 'y',
'm': 'z',
'n': 'a',
'o': 'b',
'p': 'c',
'q': 'd',
'r': 'e',
's': 'f',
't': 'g',
'u': 'h',
'v': 'i',
'w': 'j',
'x': 'k',
'y': 'l',
'z': 'm',
}
type rot13Reader struct {
reader io.Reader
}
func (rot *rot13Reader) Read(p []byte) (i int, err error) {
if len(p) == 0 {
return 0, nil
}
buf := make([]byte, len(p))
i, err = rot.reader.Read(buf)
var (
b, c byte
found bool
)
for i := range buf {
b = buf[i]
c, found = lookupTable[b]
if found {
p[i] = c
} else {
p[i] = b
}
}
return
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
fmt.Println("")
}

View File

@@ -0,0 +1,20 @@
package main
import (
"code.google.com/p/go-tour/pic"
)
func Pic(dx, dy int) [][]uint8 {
cols := make([][]uint8, dy)
for i := range cols {
cols[i] = make([]uint8, dx)
for j := range cols[i] {
cols[i][j] = uint8(j ^ i)
}
}
return cols
}
func main() {
pic.Show(Pic)
}

View File

@@ -0,0 +1,25 @@
package main
import (
"fmt"
"math"
)
// using Newton's method
func Sqrt(x float64) float64 {
z := x - (x * 0.1)
for i := 0; i < 1000; i++ {
newZ := z - (((z * z) - x) / 2 * z)
diff := math.Abs(newZ) - math.Abs(z)
if math.Abs(diff) < 0.01 {
return newZ
}
z = newZ
}
return z
}
func main() {
fmt.Printf("stdlib -> %f\n", math.Sqrt(2))
fmt.Printf("newton -> %f\n", Sqrt(2))
}

View File

@@ -0,0 +1,18 @@
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}

View File

@@ -0,0 +1,19 @@
package main
import (
"fmt"
"net/http"
)
type Hello struct{}
func (h Hello) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprint(w, "Hello!\n")
}
func main() {
var h Hello
http.ListenAndServe("localhost:4000", h)
}

View File

@@ -0,0 +1,18 @@
// From go-tour #28
package main
import "fmt"
type Vertex struct {
Lat, Long float64
}
var m map[string]Vertex
func main() {
m = make(map[string]Vertex)
m["Bell Labs"] = Vertex{
40.68433, 74.39967,
}
fmt.Println(m["Bell Labs"])
}

View File

@@ -0,0 +1,13 @@
package main
import "fmt"
func main() {
pow := make([]int, 10)
for i := range pow {
pow[i] = 1 << uint(i)
}
for _, value := range pow {
fmt.Printf("%d\n", value)
}
}

View File

@@ -0,0 +1,39 @@
package main
import (
"fmt"
"os"
"strconv"
)
func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x + y
case <- quit:
return
}
}
}
func main() {
count := 10
if len(os.Args) > 1 {
var err error
if count, err = strconv.Atoi(os.Args[1]); err != nil {
fmt.Fprintln(os.Stderr, os.Args[1], "is not an int!")
os.Exit(1)
}
}
c := make(chan int)
quit := make(chan int)
go func(count int) {
for i := 0; i < count; i++ {
fmt.Println(<-c)
}
quit <- 0
}(count)
fibonacci(c, quit)
}