TRIVIAL go fmt
This commit is contained in:
parent
090d101d51
commit
736d61e82c
@ -1,31 +1,31 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"math"
|
||||||
"math"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func sum(a []int, c chan int) {
|
func sum(a []int, c chan int) {
|
||||||
sum := 0
|
sum := 0
|
||||||
for _, v := range a {
|
for _, v := range a {
|
||||||
sum += v
|
sum += v
|
||||||
}
|
}
|
||||||
to_sleep := time.Duration(100 * math.Abs(float64(sum))) * time.Millisecond
|
to_sleep := time.Duration(100*math.Abs(float64(sum))) * time.Millisecond
|
||||||
fmt.Printf("Sleeping %s...\n", to_sleep)
|
fmt.Printf("Sleeping %s...\n", to_sleep)
|
||||||
time.Sleep(to_sleep)
|
time.Sleep(to_sleep)
|
||||||
c <- sum
|
c <- sum
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
a := []int{7, 2, 8, -9, 4, 0}
|
a := []int{7, 2, 8, -9, 4, 0}
|
||||||
|
|
||||||
c := make(chan int)
|
c := make(chan int)
|
||||||
go sum(a[:len(a)/2], c)
|
go sum(a[:len(a)/2], c)
|
||||||
go sum(a[len(a)/2:], c)
|
go sum(a[len(a)/2:], c)
|
||||||
x := <-c
|
x := <-c
|
||||||
fmt.Printf("x = %d\n", x)
|
fmt.Printf("x = %d\n", x)
|
||||||
y := <-c
|
y := <-c
|
||||||
fmt.Printf("y = %d\n", y)
|
fmt.Printf("y = %d\n", y)
|
||||||
fmt.Println(x, y, x + y)
|
fmt.Println(x, y, x+y)
|
||||||
}
|
}
|
||||||
|
@ -5,19 +5,19 @@ import "fmt"
|
|||||||
// fibonacci is a function that returns
|
// fibonacci is a function that returns
|
||||||
// a function that returns an int.
|
// a function that returns an int.
|
||||||
func fibonacci() func() int {
|
func fibonacci() func() int {
|
||||||
prev := 0
|
prev := 0
|
||||||
cur := 1
|
cur := 1
|
||||||
return func() int {
|
return func() int {
|
||||||
old_cur := cur
|
old_cur := cur
|
||||||
cur = prev + cur
|
cur = prev + cur
|
||||||
prev = old_cur
|
prev = old_cur
|
||||||
return cur
|
return cur
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
f := fibonacci()
|
f := fibonacci()
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
fmt.Println(f())
|
fmt.Println(f())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,32 +1,32 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type String string
|
type String string
|
||||||
|
|
||||||
type Struct struct {
|
type Struct struct {
|
||||||
Greeting string
|
Greeting string
|
||||||
Punct string
|
Punct string
|
||||||
Who string
|
Who string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s String) ServeHTTP(
|
func (s String) ServeHTTP(
|
||||||
w http.ResponseWriter,
|
w http.ResponseWriter,
|
||||||
r *http.Request) {
|
r *http.Request) {
|
||||||
fmt.Fprintf(w, "%s\n", s)
|
fmt.Fprintf(w, "%s\n", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Struct) ServeHTTP(
|
func (s Struct) ServeHTTP(
|
||||||
w http.ResponseWriter,
|
w http.ResponseWriter,
|
||||||
r *http.Request) {
|
r *http.Request) {
|
||||||
fmt.Fprintf(w, "%s%s %s", s.Greeting, s.Punct, s.Who)
|
fmt.Fprintf(w, "%s%s %s", s.Greeting, s.Punct, s.Who)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.Handle("/string", String("I'm a frayed knot."))
|
http.Handle("/string", String("I'm a frayed knot."))
|
||||||
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
|
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
|
||||||
http.ListenAndServe("localhost:4000", nil)
|
http.ListenAndServe("localhost:4000", nil)
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"code.google.com/p/go-tour/wc"
|
||||||
"code.google.com/p/go-tour/wc"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WordCount(s string) map[string]int {
|
func WordCount(s string) map[string]int {
|
||||||
counts := make(map[string]int)
|
counts := make(map[string]int)
|
||||||
words := strings.Fields(s)
|
words := strings.Fields(s)
|
||||||
for _, word := range words {
|
for _, word := range words {
|
||||||
counts[word]++
|
counts[word]++
|
||||||
}
|
}
|
||||||
return counts
|
return counts
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
wc.Test(WordCount)
|
wc.Test(WordCount)
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.google.com/p/go-tour/pic"
|
"code.google.com/p/go-tour/pic"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Pic(dx, dy int) [][]uint8 {
|
func Pic(dx, dy int) [][]uint8 {
|
||||||
cols := make([][]uint8, dy)
|
cols := make([][]uint8, dy)
|
||||||
for i := range cols {
|
for i := range cols {
|
||||||
cols[i] = make([]uint8, dx)
|
cols[i] = make([]uint8, dx)
|
||||||
for j := range cols[i] {
|
for j := range cols[i] {
|
||||||
cols[i][j] = uint8(j^i)
|
cols[i][j] = uint8(j ^ i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cols
|
return cols
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pic.Show(Pic)
|
pic.Show(Pic)
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,25 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
// using Newton's method
|
// using Newton's method
|
||||||
func Sqrt(x float64) float64 {
|
func Sqrt(x float64) float64 {
|
||||||
z := x - (x * 0.1)
|
z := x - (x * 0.1)
|
||||||
for i := 0 ; i < 1000 ; i++ {
|
for i := 0; i < 1000; i++ {
|
||||||
newZ := z - (((z * z) - x) / 2 * z)
|
newZ := z - (((z * z) - x) / 2 * z)
|
||||||
diff := math.Abs(newZ) - math.Abs(z)
|
diff := math.Abs(newZ) - math.Abs(z)
|
||||||
if math.Abs(diff) < 0.01 {
|
if math.Abs(diff) < 0.01 {
|
||||||
return newZ
|
return newZ
|
||||||
}
|
}
|
||||||
z = newZ
|
z = newZ
|
||||||
}
|
}
|
||||||
return z
|
return z
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Printf("stdlib -> %f\n", math.Sqrt(2))
|
fmt.Printf("stdlib -> %f\n", math.Sqrt(2))
|
||||||
fmt.Printf("newton -> %f\n", Sqrt(2))
|
fmt.Printf("newton -> %f\n", Sqrt(2))
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func say(s string) {
|
func say(s string) {
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
fmt.Println(s)
|
fmt.Println(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
go say("world")
|
go say("world")
|
||||||
say("hello")
|
say("hello")
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Hello struct{}
|
type Hello struct{}
|
||||||
|
|
||||||
func (h Hello) ServeHTTP(
|
func (h Hello) ServeHTTP(
|
||||||
w http.ResponseWriter,
|
w http.ResponseWriter,
|
||||||
r *http.Request) {
|
r *http.Request) {
|
||||||
fmt.Fprint(w, "Hello!\n")
|
fmt.Fprint(w, "Hello!\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var h Hello
|
var h Hello
|
||||||
http.ListenAndServe("localhost:4000", h)
|
http.ListenAndServe("localhost:4000", h)
|
||||||
}
|
}
|
||||||
|
@ -4,15 +4,15 @@ package main
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
type Vertex struct {
|
type Vertex struct {
|
||||||
Lat, Long float64
|
Lat, Long float64
|
||||||
}
|
}
|
||||||
|
|
||||||
var m map[string]Vertex
|
var m map[string]Vertex
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
m = make(map[string]Vertex)
|
m = make(map[string]Vertex)
|
||||||
m["Bell Labs"] = Vertex{
|
m["Bell Labs"] = Vertex{
|
||||||
40.68433, 74.39967,
|
40.68433, 74.39967,
|
||||||
}
|
}
|
||||||
fmt.Println(m["Bell Labs"])
|
fmt.Println(m["Bell Labs"])
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,11 @@ package main
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pow := make([]int, 10)
|
pow := make([]int, 10)
|
||||||
for i := range pow {
|
for i := range pow {
|
||||||
pow[i] = 1 << uint(i)
|
pow[i] = 1 << uint(i)
|
||||||
}
|
}
|
||||||
for _, value := range pow {
|
for _, value := range pow {
|
||||||
fmt.Printf("%d\n", value)
|
fmt.Printf("%d\n", value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user