Collapsing tree into more sane Go project layout
since I was in here looking at something unrelated...
This commit is contained in:
29
gotime/gotour-artifacts/buffered-channels/main.go
Normal file
29
gotime/gotour-artifacts/buffered-channels/main.go
Normal 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
|
||||
}
|
31
gotime/gotour-artifacts/channels/main.go
Normal file
31
gotime/gotour-artifacts/channels/main.go
Normal 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)
|
||||
}
|
23
gotime/gotour-artifacts/exercise-fibonacci-closure/main.go
Normal file
23
gotime/gotour-artifacts/exercise-fibonacci-closure/main.go
Normal 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())
|
||||
}
|
||||
}
|
32
gotime/gotour-artifacts/exercise-http-handlers/main.go
Normal file
32
gotime/gotour-artifacts/exercise-http-handlers/main.go
Normal 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)
|
||||
}
|
19
gotime/gotour-artifacts/exercise-maps/main.go
Normal file
19
gotime/gotour-artifacts/exercise-maps/main.go
Normal 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)
|
||||
}
|
97
gotime/gotour-artifacts/exercise-rot13-reader/main.go
Normal file
97
gotime/gotour-artifacts/exercise-rot13-reader/main.go
Normal 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("")
|
||||
}
|
20
gotime/gotour-artifacts/exercise-slices/main.go
Normal file
20
gotime/gotour-artifacts/exercise-slices/main.go
Normal 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)
|
||||
}
|
25
gotime/gotour-artifacts/exercise-sqrt/main.go
Normal file
25
gotime/gotour-artifacts/exercise-sqrt/main.go
Normal 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))
|
||||
}
|
18
gotime/gotour-artifacts/goroutines/main.go
Normal file
18
gotime/gotour-artifacts/goroutines/main.go
Normal 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")
|
||||
}
|
19
gotime/gotour-artifacts/hello-web/main.go
Normal file
19
gotime/gotour-artifacts/hello-web/main.go
Normal 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)
|
||||
}
|
18
gotime/gotour-artifacts/maps/main.go
Normal file
18
gotime/gotour-artifacts/maps/main.go
Normal 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"])
|
||||
}
|
13
gotime/gotour-artifacts/ranges/main.go
Normal file
13
gotime/gotour-artifacts/ranges/main.go
Normal 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)
|
||||
}
|
||||
}
|
39
gotime/gotour-artifacts/selects/main.go
Normal file
39
gotime/gotour-artifacts/selects/main.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user