TRIVIAL go fmt

This commit is contained in:
Dan Buch 2012-11-05 21:00:35 -05:00
parent 090d101d51
commit 736d61e82c
10 changed files with 107 additions and 107 deletions

View File

@ -1,31 +1,31 @@
package main
import (
"fmt"
"time"
"math"
"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
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}
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)
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

@ -5,19 +5,19 @@ 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
}
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())
}
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}

View File

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

View File

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

View File

@ -1,20 +1,20 @@
package main
import (
"code.google.com/p/go-tour/pic"
"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
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)
pic.Show(Pic)
}

View File

@ -1,25 +1,25 @@
package main
import (
"fmt"
"math"
"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
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))
fmt.Printf("stdlib -> %f\n", math.Sqrt(2))
fmt.Printf("newton -> %f\n", Sqrt(2))
}

View File

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

View File

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

View File

@ -4,15 +4,15 @@ package main
import "fmt"
type Vertex struct {
Lat, Long float64
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"])
m = make(map[string]Vertex)
m["Bell Labs"] = Vertex{
40.68433, 74.39967,
}
fmt.Println(m["Bell Labs"])
}

View File

@ -3,11 +3,11 @@ 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)
}
pow := make([]int, 10)
for i := range pow {
pow[i] = 1 << uint(i)
}
for _, value := range pow {
fmt.Printf("%d\n", value)
}
}