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"
func main() {
slice1 := []int{1,2,3}
slice1 := []int{1, 2, 3}
slice2 := append(slice1, 4, 5)
fmt.Println(slice1)
fmt.Println(slice2)

View File

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

View File

@ -10,7 +10,7 @@ type Circle struct {
}
func (c *Circle) area() float64 {
return math.Pi * c.r*c.r
return math.Pi * c.r * c.r
}
type Rectangle struct {
@ -32,7 +32,7 @@ func (r *Rectangle) area() float64 {
func main() {
circle := Circle{x: 0, y: 3, r: 5}
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())
}

View File

@ -14,7 +14,7 @@ type Circle struct {
}
func (c *Circle) area() float64 {
return math.Pi * c.r*c.r
return math.Pi * c.r * c.r
}
type Rectangle struct {
@ -43,7 +43,7 @@ func totalArea(shapes ...Shape) float64 {
func main() {
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
for _, s := range []Shape{&circle, &rectangle} {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -16,7 +16,7 @@ func main() {
p(strings.HasPrefix("test", "te"))
p(strings.HasSuffix("test", "st"))
p(strings.Index("test", "e"))
p(strings.Join([]string{"a","b"}, "-"))
p(strings.Join([]string{"a", "b"}, "-"))
p(strings.Repeat("a", 5))
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"]}`)
var dat map[string]interface{}
err := json.Unmarshal(byt, &dat)
if err != nil { panic(err) }
if err != nil {
panic(err)
}
fmt.Println(dat)
name := dat["Name"].(string)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -9,16 +9,22 @@ import "fmt"
func main() {
db, openErr := sql.Open("postgres", "dbname=gobyexample sslmode=disable")
if openErr != nil { panic(openErr) }
if openErr != nil {
panic(openErr)
}
defer db.Close()
fmt.Println(db)
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)
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)
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)",
3, 7.0, true, "more", t1,
5, 1.0, false, "less", t2)
if minsertErr != nil { panic(minsertErr) }
if minsertErr != nil {
panic(minsertErr)
}
num, _ := minsertRep.RowsAffected()
fmt.Println(num)
rows, selectErr := db.Query("SELECT * FROM items")
if selectErr != nil { panic(selectErr) }
if selectErr != nil {
panic(selectErr)
}
defer rows.Close()
for rows.Next() {
var r1 int
@ -43,10 +53,14 @@ func main() {
fmt.Println(r1, r2, r3, r4, r5)
}
rowsErr := rows.Err()
if rowsErr != nil { panic(rowsErr) }
if rowsErr != nil {
panic(rowsErr)
}
dropRep, dropErr := db.Exec("DROP TABLE items")
if dropErr != nil { panic(dropErr) }
if dropErr != nil {
panic(dropErr)
}
fmt.Println(dropRep)
}

View File

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

View File

@ -7,7 +7,7 @@ import "net/http"
import "fmt"
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() {

View File

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

View File

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

View File

@ -12,5 +12,7 @@ func handler(res http.ResponseWriter, req *http.Request) {
func main() {
http.HandleFunc("/", handler)
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 (
"fmt"
"io/ioutil"
"strings"
"regexp"
"os"
"regexp"
"sort"
"strings"
)
func minInt(a, b int) int {
if (a < b) {
if a < b {
return a
}
return b
@ -24,7 +24,9 @@ func main() {
sourceNames := make([]string, 0)
sourceMap := make(map[string]string)
fileInfos, dirErr := ioutil.ReadDir("./")
if dirErr != nil { panic(dirErr) }
if dirErr != nil {
panic(dirErr)
}
baseTrimmer, _ := regexp.Compile("[0-9x]+-")
for _, fi := range fileInfos {
baseName := baseTrimmer.ReplaceAllString(fi.Name(), "")
@ -36,7 +38,9 @@ func main() {
// read names from index
indexBytes, idxErr := ioutil.ReadFile("tool/index.txt")
if idxErr != nil { panic (idxErr) }
if idxErr != nil {
panic(idxErr)
}
indexNamesAll := strings.Split(string(indexBytes), "\n")
indexNames := make([]string, 0)
for _, indexName := range indexNamesAll {