gofmt wat u know about it

This commit is contained in:
Mark McGranaghan 2012-09-23 18:31:33 -07:00
parent f93bdba75c
commit 89f95f2ac5
66 changed files with 729 additions and 678 deletions

View File

@ -5,7 +5,7 @@ package main
import "fmt" import "fmt"
func main() { func main() {
slice1 := []int{1,2,3} slice1 := []int{1, 2, 3}
slice2 := append(slice1, 4, 5) slice2 := append(slice1, 4, 5)
fmt.Println(slice1) fmt.Println(slice1)
fmt.Println(slice2) fmt.Println(slice2)

View File

@ -13,7 +13,7 @@ func avg(vals []float64) float64 {
} }
func main() { func main() {
input := []float64{98,93,77,82,83} input := []float64{98, 93, 77, 82, 83}
fmt.Println(input) fmt.Println(input)
output := avg(input) output := avg(input)
fmt.Println(output) fmt.Println(output)

View File

@ -10,7 +10,7 @@ type Circle struct {
} }
func (c *Circle) area() float64 { func (c *Circle) area() float64 {
return math.Pi * c.r*c.r return math.Pi * c.r * c.r
} }
type Rectangle struct { type Rectangle struct {
@ -32,7 +32,7 @@ func (r *Rectangle) area() float64 {
func main() { func main() {
circle := Circle{x: 0, y: 3, r: 5} circle := Circle{x: 0, y: 3, r: 5}
fmt.Println(circle.area()) fmt.Println(circle.area())
rectangle := Rectangle {x1: 3, x2: 10, y1: 5, y2: 7} rectangle := Rectangle{x1: 3, x2: 10, y1: 5, y2: 7}
fmt.Println(rectangle.area()) fmt.Println(rectangle.area())
} }

View File

@ -14,7 +14,7 @@ type Circle struct {
} }
func (c *Circle) area() float64 { func (c *Circle) area() float64 {
return math.Pi * c.r*c.r return math.Pi * c.r * c.r
} }
type Rectangle struct { type Rectangle struct {
@ -43,7 +43,7 @@ func totalArea(shapes ...Shape) float64 {
func main() { func main() {
circle := Circle{x: 0, y: 3, r: 5} circle := Circle{x: 0, y: 3, r: 5}
rectangle := Rectangle {x1: 3, x2: 10, y1: 5, y2: 7} rectangle := Rectangle{x1: 3, x2: 10, y1: 5, y2: 7}
area := 0.0 area := 0.0
for _, s := range []Shape{&circle, &rectangle} { for _, s := range []Shape{&circle, &rectangle} {

View File

@ -2,7 +2,10 @@
package main package main
import ("fmt"; "errors") import (
"errors"
"fmt"
)
func myFun(arg int) (int, error) { func myFun(arg int) (int, error) {
if arg == 42 { if arg == 42 {

View File

@ -6,7 +6,7 @@ import "fmt"
func main() { func main() {
var messages chan string = make(chan string) var messages chan string = make(chan string)
go func() { messages <- "ping"}() go func() { messages <- "ping" }()
msg := <- messages msg := <-messages
fmt.Println(msg) fmt.Println(msg)
} }

View File

@ -7,6 +7,6 @@ import "fmt"
func main() { func main() {
var messages chan string = make(chan string, 1) var messages chan string = make(chan string, 1)
messages <- "ping" messages <- "ping"
msg := <- messages msg := <-messages
fmt.Println(msg) fmt.Println(msg)
} }

View File

@ -12,14 +12,14 @@ func pinger(pings chan<- string) {
func ponger(pings <-chan string, pongs chan<- string) { func ponger(pings <-chan string, pongs chan<- string) {
for { for {
<- pings <-pings
pongs <- "pong" pongs <- "pong"
} }
} }
func printer(pongs <-chan string) { func printer(pongs <-chan string) {
for { for {
msg := <- pongs msg := <-pongs
fmt.Println(msg) fmt.Println(msg)
} }
} }

View File

@ -28,5 +28,5 @@ func main() {
// Block until we can receive a value from the worker // Block until we can receive a value from the worker
// over the channel. // over the channel.
<- done <-done
} }

View File

@ -20,15 +20,15 @@ func main() {
}() }()
go func() { go func() {
for i :=0; i < 2; i ++ { for i := 0; i < 2; i++ {
select { select {
case msg1 := <- c1: case msg1 := <-c1:
fmt.Println(msg1) fmt.Println(msg1)
case msg2 := <- c2: case msg2 := <-c2:
fmt.Println(msg2) fmt.Println(msg2)
} }
} }
d <- true d <- true
}() }()
<- d <-d
} }

View File

@ -16,12 +16,12 @@ func main() {
go func() { go func() {
select { select {
case msg := <- c: case msg := <-c:
fmt.Println(msg) fmt.Println(msg)
case <- time.After(time.Millisecond * 1000): case <-time.After(time.Millisecond * 1000):
fmt.Println("timeout") fmt.Println("timeout")
} }
d <- true d <- true
}() }()
<- d <-d
} }

View File

@ -7,7 +7,7 @@ import "fmt"
func main() { func main() {
throttle := time.Tick(time.Millisecond * 200) throttle := time.Tick(time.Millisecond * 200)
for { for {
<- throttle <-throttle
go fmt.Println("rate-limited action") go fmt.Println("rate-limited action")
} }
} }

View File

@ -7,14 +7,14 @@ import "fmt"
func main() { func main() {
timer1 := time.NewTimer(time.Second) timer1 := time.NewTimer(time.Second)
<- timer1.C <-timer1.C
fmt.Println("Timer 1 expired") fmt.Println("Timer 1 expired")
stop1 := timer1.Stop() stop1 := timer1.Stop()
fmt.Println("Timer 2 stopped:", stop1) fmt.Println("Timer 2 stopped:", stop1)
timer2 := time.NewTimer(time.Second) timer2 := time.NewTimer(time.Second)
go func() { go func() {
<- timer2.C <-timer2.C
fmt.Println("Timer 2 expired") fmt.Println("Timer 2 expired")
}() }()
stop2 := timer2.Stop() stop2 := timer2.Stop()

View File

@ -2,7 +2,10 @@
package main package main
import ("fmt" ; "sort") import (
"fmt"
"sort"
)
type Person struct { type Person struct {
Name string Name string
@ -10,6 +13,7 @@ type Person struct {
} }
type ByName []Person type ByName []Person
func (this ByName) Len() int { func (this ByName) Len() int {
return len(this) return len(this)
} }
@ -21,6 +25,7 @@ func (this ByName) Swap(i, j int) {
} }
type ByAge []Person type ByAge []Person
func (this ByAge) Len() int { func (this ByAge) Len() int {
return len(this) return len(this)
} }

View File

@ -18,7 +18,7 @@ func Include(elems []string, val string) bool {
return Index(elems, val) >= 0 return Index(elems, val) >= 0
} }
func Any(elems []string, f func(string)bool) bool { func Any(elems []string, f func(string) bool) bool {
for _, v := range elems { for _, v := range elems {
if f(v) { if f(v) {
return true return true
@ -27,7 +27,7 @@ func Any(elems []string, f func(string)bool) bool {
return false return false
} }
func All(elems []string, f func(string)bool) bool { func All(elems []string, f func(string) bool) bool {
for _, v := range elems { for _, v := range elems {
if !f(v) { if !f(v) {
return false return false
@ -36,7 +36,7 @@ func All(elems []string, f func(string)bool) bool {
return true return true
} }
func Filter(elems []string, f func(string)bool) []string { func Filter(elems []string, f func(string) bool) []string {
filtered := []string{} filtered := []string{}
for _, v := range elems { for _, v := range elems {
if f(v) { if f(v) {
@ -46,7 +46,7 @@ func Filter(elems []string, f func(string)bool) []string {
return filtered return filtered
} }
func Map(elems []string, f func(string)string) []string { func Map(elems []string, f func(string) string) []string {
mapped := make([]string, len(elems)) mapped := make([]string, len(elems))
for i, v := range elems { for i, v := range elems {
mapped[i] = f(v) mapped[i] = f(v)

View File

@ -16,7 +16,7 @@ func main() {
p(strings.HasPrefix("test", "te")) p(strings.HasPrefix("test", "te"))
p(strings.HasSuffix("test", "st")) p(strings.HasSuffix("test", "st"))
p(strings.Index("test", "e")) p(strings.Index("test", "e"))
p(strings.Join([]string{"a","b"}, "-")) p(strings.Join([]string{"a", "b"}, "-"))
p(strings.Repeat("a", 5)) p(strings.Repeat("a", 5))
p(strings.Replace("foo", "o", "0", -1)) p(strings.Replace("foo", "o", "0", -1))
p(strings.Replace("foo", "o", "0", 1)) p(strings.Replace("foo", "o", "0", 1))

View File

@ -26,7 +26,9 @@ func main() {
byt := []byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`) byt := []byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`)
var dat map[string]interface{} var dat map[string]interface{}
err := json.Unmarshal(byt, &dat) err := json.Unmarshal(byt, &dat)
if err != nil { panic(err) } if err != nil {
panic(err)
}
fmt.Println(dat) fmt.Println(dat)
name := dat["Name"].(string) name := dat["Name"].(string)

View File

@ -13,5 +13,4 @@ func main() {
fmt.Print(string(contents)) fmt.Print(string(contents))
} }
// todo: streaming reads // todo: streaming reads

View File

@ -17,7 +17,6 @@ func main() {
// with normal indexing. // with normal indexing.
arg := os.Args[3] arg := os.Args[3]
fmt.Println(argsWithProg) fmt.Println(argsWithProg)
fmt.Println(argsWithoutProg) fmt.Println(argsWithoutProg)
fmt.Println(arg) fmt.Println(arg)

View File

@ -2,21 +2,26 @@
package main package main
import ("fmt"; "os"; "os/signal"; "syscall") import (
"fmt"
"os"
"os/signal"
"syscall"
)
func main() { func main() {
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
d := make(chan bool, 1) d := make(chan bool, 1)
signal.Notify(c, syscall.SIGINT) signal.Notify(c, syscall.SIGINT)
go func(){ go func() {
sig := <- c sig := <-c
fmt.Println() fmt.Println()
fmt.Println(sig) fmt.Println(sig)
d <- true d <- true
}() }()
fmt.Println("Awaiting signal") fmt.Println("Awaiting signal")
<- d <-d
} }
// todo: sending signals? // todo: sending signals?

View File

@ -13,7 +13,9 @@ func main() {
} }
client := &http.Client{Transport: tr} client := &http.Client{Transport: tr}
resp, err := client.Get("https://127.0.0.1:5000/") resp, err := client.Get("https://127.0.0.1:5000/")
if err != nil { panic(err) } if err != nil {
panic(err)
}
defer resp.Body.Close() defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
fmt.Print(string(body)) fmt.Print(string(body))

View File

@ -15,10 +15,14 @@ func main() {
// set & get // set & get
setRep := client.Set("foo", "bar") setRep := client.Set("foo", "bar")
if setRep.Err != nil { panic(setRep.Err) } if setRep.Err != nil {
panic(setRep.Err)
}
fmt.Println(setRep) fmt.Println(setRep)
getRep := client.Get("foo") getRep := client.Get("foo")
if getRep.Err != nil { panic(getRep.Err) } if getRep.Err != nil {
panic(getRep.Err)
}
getStr, _ := getRep.Str() getStr, _ := getRep.Str()
fmt.Println(getRep) fmt.Println(getRep)
fmt.Println(getStr) fmt.Println(getStr)
@ -35,7 +39,9 @@ func main() {
mc.Set("k1", "v1") mc.Set("k1", "v1")
mc.Get("k1") mc.Get("k1")
}) })
if mcallRep.Err != nil { panic(mcallRep.Err) } if mcallRep.Err != nil {
panic(mcallRep.Err)
}
mcallVal, _ := mcallRep.Elems[1].Str() mcallVal, _ := mcallRep.Elems[1].Str()
fmt.Println(mcallVal) fmt.Println(mcallVal)
@ -44,7 +50,9 @@ func main() {
mc.Set("k2", "v2") mc.Set("k2", "v2")
mc.Get("k2") mc.Get("k2")
}) })
if tranRep.Err != nil { panic(tranRep.Err) } if tranRep.Err != nil {
panic(tranRep.Err)
}
tranStr, _ := tranRep.Elems[1].Str() tranStr, _ := tranRep.Elems[1].Str()
fmt.Println(tranStr) fmt.Println(tranStr)
@ -53,7 +61,9 @@ func main() {
fmt.Println(msg) fmt.Println(msg)
} }
sub, subErr := client.Subscription(msgHdlr) sub, subErr := client.Subscription(msgHdlr)
if subErr != nil { panic(subErr) } if subErr != nil {
panic(subErr)
}
defer sub.Close() defer sub.Close()
sub.Subscribe("chan1", "chan2") sub.Subscribe("chan1", "chan2")
sub.Psubscribe("chan*") sub.Psubscribe("chan*")

View File

@ -9,16 +9,22 @@ import "fmt"
func main() { func main() {
db, openErr := sql.Open("postgres", "dbname=gobyexample sslmode=disable") db, openErr := sql.Open("postgres", "dbname=gobyexample sslmode=disable")
if openErr != nil { panic(openErr) } if openErr != nil {
panic(openErr)
}
defer db.Close() defer db.Close()
fmt.Println(db) fmt.Println(db)
createRep, createErr := db.Exec("CREATE TABLE items (a int, b float, c boolean, d text, e timestamp with time zone)") createRep, createErr := db.Exec("CREATE TABLE items (a int, b float, c boolean, d text, e timestamp with time zone)")
if createErr != nil { panic(createErr) } if createErr != nil {
panic(createErr)
}
fmt.Println(createRep) fmt.Println(createRep)
insertRep, insertErr := db.Exec("INSERT INTO items VALUES (1, 2.0, false, 'string', '2000-01-01T01:02:03Z')") insertRep, insertErr := db.Exec("INSERT INTO items VALUES (1, 2.0, false, 'string', '2000-01-01T01:02:03Z')")
if insertErr != nil { panic(insertErr) } if insertErr != nil {
panic(insertErr)
}
fmt.Println(insertRep) fmt.Println(insertRep)
t1, _ := time.Parse(time.RFC3339, "2000-04-08T03:02:01Z") t1, _ := time.Parse(time.RFC3339, "2000-04-08T03:02:01Z")
@ -26,12 +32,16 @@ func main() {
minsertRep, minsertErr := db.Exec("Insert INTO items VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10)", minsertRep, minsertErr := db.Exec("Insert INTO items VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10)",
3, 7.0, true, "more", t1, 3, 7.0, true, "more", t1,
5, 1.0, false, "less", t2) 5, 1.0, false, "less", t2)
if minsertErr != nil { panic(minsertErr) } if minsertErr != nil {
panic(minsertErr)
}
num, _ := minsertRep.RowsAffected() num, _ := minsertRep.RowsAffected()
fmt.Println(num) fmt.Println(num)
rows, selectErr := db.Query("SELECT * FROM items") rows, selectErr := db.Query("SELECT * FROM items")
if selectErr != nil { panic(selectErr) } if selectErr != nil {
panic(selectErr)
}
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var r1 int var r1 int
@ -43,10 +53,14 @@ func main() {
fmt.Println(r1, r2, r3, r4, r5) fmt.Println(r1, r2, r3, r4, r5)
} }
rowsErr := rows.Err() rowsErr := rows.Err()
if rowsErr != nil { panic(rowsErr) } if rowsErr != nil {
panic(rowsErr)
}
dropRep, dropErr := db.Exec("DROP TABLE items") dropRep, dropErr := db.Exec("DROP TABLE items")
if dropErr != nil { panic(dropErr) } if dropErr != nil {
panic(dropErr)
}
fmt.Println(dropRep) fmt.Println(dropRep)
} }

View File

@ -19,7 +19,9 @@ func main() {
[]string{"mark@heroku.com"}, []string{"mark@heroku.com"},
[]byte("nThe body."), []byte("nThe body."),
) )
if err != nil { panic(err) } if err != nil {
panic(err)
}
} }
// todo: missing subject, cc, bcc // todo: missing subject, cc, bcc

View File

@ -7,7 +7,7 @@ import "net/http"
import "fmt" import "fmt"
func hello(w http.ResponseWriter, req *http.Request) { func hello(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "hello " + req.URL.Query().Get(":name")) fmt.Fprintln(w, "hello "+req.URL.Query().Get(":name"))
} }
func main() { func main() {

View File

@ -3,10 +3,10 @@
package main package main
import ( import (
"net/http"
"encoding/base64" "encoding/base64"
"strings"
"fmt" "fmt"
"net/http"
"strings"
) )
type Authenticator func(string, string) bool type Authenticator func(string, string) bool

View File

@ -3,14 +3,14 @@
package main package main
import ( import (
"fmt"
"net" "net"
"net/http" "net/http"
"time"
"os" "os"
"os/signal" "os/signal"
"syscall"
"fmt"
"sync/atomic" "sync/atomic"
"syscall"
"time"
) )
func slow(res http.ResponseWriter, req *http.Request) { func slow(res http.ResponseWriter, req *http.Request) {
@ -52,22 +52,26 @@ func main() {
server := &http.Server{Handler: http.HandlerFunc(slow)} server := &http.Server{Handler: http.HandlerFunc(slow)}
fmt.Println("listen at=start") fmt.Println("listen at=start")
listener, listenErr := net.Listen("tcp", ":5000") listener, listenErr := net.Listen("tcp", ":5000")
if listenErr != nil { panic(listenErr) } if listenErr != nil {
panic(listenErr)
}
wListener := &watchedListener{Listener: listener} wListener := &watchedListener{Listener: listener}
fmt.Println("listen at=finish") fmt.Println("listen at=finish")
go func() { go func() {
<- stop <-stop
fmt.Println("close at=start") fmt.Println("close at=start")
closeErr := wListener.Close() closeErr := wListener.Close()
if closeErr != nil { panic(closeErr) } if closeErr != nil {
panic(closeErr)
}
fmt.Println("close at=finish") fmt.Println("close at=finish")
}() }()
go func() { go func() {
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
fmt.Println("trap at=start") fmt.Println("trap at=start")
<- sig <-sig
stop <- true stop <- true
fmt.Println("trap at=finish") fmt.Println("trap at=finish")
}() }()

View File

@ -12,5 +12,7 @@ func handler(res http.ResponseWriter, req *http.Request) {
func main() { func main() {
http.HandleFunc("/", handler) http.HandleFunc("/", handler)
err := http.ListenAndServeTLS(":5000", "/tmp/server.crt", "/tmp/server.key", nil) err := http.ListenAndServeTLS(":5000", "/tmp/server.crt", "/tmp/server.key", nil)
if err != nil { panic(err) } if err != nil {
panic(err)
}
} }

View File

@ -6,14 +6,14 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strings"
"regexp"
"os" "os"
"regexp"
"sort" "sort"
"strings"
) )
func minInt(a, b int) int { func minInt(a, b int) int {
if (a < b) { if a < b {
return a return a
} }
return b return b
@ -24,7 +24,9 @@ func main() {
sourceNames := make([]string, 0) sourceNames := make([]string, 0)
sourceMap := make(map[string]string) sourceMap := make(map[string]string)
fileInfos, dirErr := ioutil.ReadDir("./") fileInfos, dirErr := ioutil.ReadDir("./")
if dirErr != nil { panic(dirErr) } if dirErr != nil {
panic(dirErr)
}
baseTrimmer, _ := regexp.Compile("[0-9x]+-") baseTrimmer, _ := regexp.Compile("[0-9x]+-")
for _, fi := range fileInfos { for _, fi := range fileInfos {
baseName := baseTrimmer.ReplaceAllString(fi.Name(), "") baseName := baseTrimmer.ReplaceAllString(fi.Name(), "")
@ -36,7 +38,9 @@ func main() {
// read names from index // read names from index
indexBytes, idxErr := ioutil.ReadFile("tool/index.txt") indexBytes, idxErr := ioutil.ReadFile("tool/index.txt")
if idxErr != nil { panic (idxErr) } if idxErr != nil {
panic(idxErr)
}
indexNamesAll := strings.Split(string(indexBytes), "\n") indexNamesAll := strings.Split(string(indexBytes), "\n")
indexNames := make([]string, 0) indexNames := make([]string, 0)
for _, indexName := range indexNamesAll { for _, indexName := range indexNamesAll {