From 9a3987c96a32f2ecab88506d4a4f88d9653b87cd Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Mon, 1 Oct 2012 08:01:39 -0700 Subject: [PATCH] gofmt --- README.md | 8 - .../nonblocking-channel-operations.go | 52 +++--- src/040-closing-channels/closing-channels.go | 44 ++--- src/043-state-goroutine/state-goroutine.go | 90 +++++----- src/044-state-mutex/state-mutex.go | 60 +++---- src/060-base64-encoding/base64-encoding.go | 30 ++-- tool/generate.go | 168 ------------------ tool/gofmt | 2 +- 8 files changed, 139 insertions(+), 315 deletions(-) delete mode 100644 tool/generate.go diff --git a/README.md b/README.md index 70f1cfc..caff62d 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,3 @@ $ go run tool/number.go ```console $ tool/gofmt ``` - - -### Golit'ing - -```console -$ go run tool/generate.go tool/generate > build/generate.html -$ open build/generate.html -``` diff --git a/src/039-nonblocking-channel-operations/nonblocking-channel-operations.go b/src/039-nonblocking-channel-operations/nonblocking-channel-operations.go index 726e0f0..ad13299 100644 --- a/src/039-nonblocking-channel-operations/nonblocking-channel-operations.go +++ b/src/039-nonblocking-channel-operations/nonblocking-channel-operations.go @@ -3,33 +3,33 @@ package main import "fmt" func main() { - messages := make(chan string) - signals := make(chan bool) + messages := make(chan string) + signals := make(chan bool) - // Non-blocking receive. - select { - case msg := <- messages: - fmt.Println("received message", msg) - default: - fmt.Println("no messages received") - } + // Non-blocking receive. + select { + case msg := <-messages: + fmt.Println("received message", msg) + default: + fmt.Println("no messages received") + } - // Non-blocking send. - msg := "hi" - select { - case messages <- msg: - fmt.Println("sent message", msg) - default: - fmt.Println("no messages sent") - } + // Non-blocking send. + msg := "hi" + select { + case messages <- msg: + fmt.Println("sent message", msg) + default: + fmt.Println("no messages sent") + } - // Non-blocking multi-way select. - select { - case msg := <- messages: - fmt.Println("received message", msg) - case sig := <- signals: - fmt.Println("received signal", sig) - default: - fmt.Println("no activity") - } + // Non-blocking multi-way select. + select { + case msg := <-messages: + fmt.Println("received message", msg) + case sig := <-signals: + fmt.Println("received signal", sig) + default: + fmt.Println("no activity") + } } diff --git a/src/040-closing-channels/closing-channels.go b/src/040-closing-channels/closing-channels.go index 7cd7e89..1758887 100644 --- a/src/040-closing-channels/closing-channels.go +++ b/src/040-closing-channels/closing-channels.go @@ -4,30 +4,30 @@ import "fmt" import "time" func main() { - jobs := make(chan bool, 5) - done := make(chan bool) + jobs := make(chan bool, 5) + done := make(chan bool) - go func() { - for { - _, more := <- jobs - if more { - fmt.Println("received job") - } else { - fmt.Println("received all") - done <- true - return - } - } - }() + go func() { + for { + _, more := <-jobs + if more { + fmt.Println("received job") + } else { + fmt.Println("received all") + done <- true + return + } + } + }() - for i := 0; i < 5 ; i++ { - jobs <- false - fmt.Println("sent job") - } + for i := 0; i < 5; i++ { + jobs <- false + fmt.Println("sent job") + } - time.Sleep(100 * time.Millisecond) - close(jobs) - fmt.Println("sent all") + time.Sleep(100 * time.Millisecond) + close(jobs) + fmt.Println("sent all") - <- done + <-done } diff --git a/src/043-state-goroutine/state-goroutine.go b/src/043-state-goroutine/state-goroutine.go index 942d91f..1069873 100644 --- a/src/043-state-goroutine/state-goroutine.go +++ b/src/043-state-goroutine/state-goroutine.go @@ -13,35 +13,35 @@ import "sync/atomic" // e.g. routing table type readOp struct { - key int - resp chan int + key int + resp chan int } type writeOp struct { - key int - val int - resp chan bool + key int + val int + resp chan bool } func randKey() int { - return rand.Intn(10) + return rand.Intn(10) } func randVal() int { - return rand.Intn(100) + return rand.Intn(100) } func manageState(reads chan *readOp, writes chan *writeOp) { - data := make(map[int]int) - for { - select { - case read := <- reads: - read.resp <- data[read.key] - case write := <- writes: - data[write.key] = write.val - write.resp <- true - } - } + data := make(map[int]int) + for { + select { + case read := <-reads: + read.resp <- data[read.key] + case write := <-writes: + data[write.key] = write.val + write.resp <- true + } + } } // Keep track of how many ops we do. @@ -49,42 +49,42 @@ var opCount int64 = 0 // Generate random reads. func generateReads(reads chan *readOp) { - for { - key := randKey() - read := &readOp{key: key, resp: make(chan int)} - reads <- read - <- read.resp - atomic.AddInt64(&opCount, 1) - } + for { + key := randKey() + read := &readOp{key: key, resp: make(chan int)} + reads <- read + <-read.resp + atomic.AddInt64(&opCount, 1) + } } // Generate random writes. func generateWrites(writes chan *writeOp) { - for { - key := randKey() - val := randVal() - write := &writeOp{key: key, val: val, resp: make(chan bool)} - writes <- write - <- write.resp - atomic.AddInt64(&opCount, 1) - } + for { + key := randKey() + val := randVal() + write := &writeOp{key: key, val: val, resp: make(chan bool)} + writes <- write + <-write.resp + atomic.AddInt64(&opCount, 1) + } } func main() { - reads := make(chan *readOp) - writes := make(chan *writeOp) + reads := make(chan *readOp) + writes := make(chan *writeOp) - go manageState(reads, writes) + go manageState(reads, writes) - for r := 0; r < 100; r++ { - go generateReads(reads) - } - for w := 0; w < 10; w++ { - go generateWrites(writes) - } + for r := 0; r < 100; r++ { + go generateReads(reads) + } + for w := 0; w < 10; w++ { + go generateWrites(writes) + } - atomic.StoreInt64(&opCount, 0) - time.Sleep(time.Second) - finalOpCount := atomic.LoadInt64(&opCount) - fmt.Println(finalOpCount) + atomic.StoreInt64(&opCount, 0) + time.Sleep(time.Second) + finalOpCount := atomic.LoadInt64(&opCount) + fmt.Println(finalOpCount) } diff --git a/src/044-state-mutex/state-mutex.go b/src/044-state-mutex/state-mutex.go index ee217d7..a78271f 100644 --- a/src/044-state-mutex/state-mutex.go +++ b/src/044-state-mutex/state-mutex.go @@ -10,11 +10,11 @@ import "sync/atomic" import "runtime" func randKey() int { - return rand.Intn(10) + return rand.Intn(10) } func randVal() int { - return rand.Intn(100) + return rand.Intn(100) } // Globally-accessible state. @@ -27,40 +27,40 @@ var opCount int64 = 0 // Generate random reads. func generateReads() { - total := 0 - for { - key := randKey() - dataMutex.Lock() - total += data[key] - dataMutex.Unlock() - atomic.AddInt64(&opCount, 1) - runtime.Gosched() - } + total := 0 + for { + key := randKey() + dataMutex.Lock() + total += data[key] + dataMutex.Unlock() + atomic.AddInt64(&opCount, 1) + runtime.Gosched() + } } // Generate random writes. func generateWrites() { - for { - key := randKey() - val := randVal() - dataMutex.Lock() - data[key] = val - dataMutex.Unlock() - atomic.AddInt64(&opCount, 1) - runtime.Gosched() - } + for { + key := randKey() + val := randVal() + dataMutex.Lock() + data[key] = val + dataMutex.Unlock() + atomic.AddInt64(&opCount, 1) + runtime.Gosched() + } } func main() { - for r := 0; r < 100; r++ { - go generateReads() - } - for w := 0; w < 10; w++ { - go generateWrites() - } + for r := 0; r < 100; r++ { + go generateReads() + } + for w := 0; w < 10; w++ { + go generateWrites() + } - atomic.StoreInt64(&opCount, 0) - time.Sleep(time.Second) - finalOpCount := atomic.LoadInt64(&opCount) - fmt.Println(finalOpCount) + atomic.StoreInt64(&opCount, 0) + time.Sleep(time.Second) + finalOpCount := atomic.LoadInt64(&opCount) + fmt.Println(finalOpCount) } diff --git a/src/060-base64-encoding/base64-encoding.go b/src/060-base64-encoding/base64-encoding.go index e1d0522..0ce3396 100644 --- a/src/060-base64-encoding/base64-encoding.go +++ b/src/060-base64-encoding/base64-encoding.go @@ -4,21 +4,21 @@ import "encoding/base64" import "fmt" func main() { - // The data we'll encode/decode. - data := "abc123!?$*&()'-=@~" - fmt.Println(data) - fmt.Println() + // The data we'll encode/decode. + data := "abc123!?$*&()'-=@~" + fmt.Println(data) + fmt.Println() - // Standard base64 encoding/decoding. - stdEnc := base64.StdEncoding.EncodeToString([]byte(data)) - fmt.Println(stdEnc) - stdDec, _ := base64.StdEncoding.DecodeString(stdEnc) - fmt.Println(string(stdDec)) - fmt.Println() + // Standard base64 encoding/decoding. + stdEnc := base64.StdEncoding.EncodeToString([]byte(data)) + fmt.Println(stdEnc) + stdDec, _ := base64.StdEncoding.DecodeString(stdEnc) + fmt.Println(string(stdDec)) + fmt.Println() - // URL base64 encoding/decoding. - urlEnc := base64.URLEncoding.EncodeToString([]byte(data)) - fmt.Println(urlEnc) - urlDec, _ := base64.URLEncoding.DecodeString(urlEnc) - fmt.Println(string(urlDec)) + // URL base64 encoding/decoding. + urlEnc := base64.URLEncoding.EncodeToString([]byte(data)) + fmt.Println(urlEnc) + urlDec, _ := base64.URLEncoding.DecodeString(urlEnc) + fmt.Println(string(urlDec)) } diff --git a/tool/generate.go b/tool/generate.go deleted file mode 100644 index be4941c..0000000 --- a/tool/generate.go +++ /dev/null @@ -1,168 +0,0 @@ -// ## golit - -// **golit** generates literate-programming style HTML -// documentation from a Go source files. -package main - -import ( - "fmt" - "io/ioutil" - "os" - "os/exec" - "regexp" - "strings" -) - -// Recognize doc lines, extract their comment prefixes. -var docsPat = regexp.MustCompile("^\\s*\\/\\/\\s") - -// Recognize title prefixes, for titling web page. -var titlePat = regexp.MustCompile("^\\/\\/\\s##\\s") - -// Abort on non-nil errors. -func check(err error) { - if err != nil { - panic(err) - } -} - -// We'll implement Markdown rendering and Pygments -// syntax highlighting by piping the source data through -// external programs. This is a general helper for -// handling both cases. -func pipe(bin string, arg []string, src string) string { - cmd := exec.Command(bin, arg...) - in, err := cmd.StdinPipe() - check(err) - out, err := cmd.StdoutPipe() - check(err) - err = cmd.Start() - check(err) - in.Write([]byte(src)) - check(err) - err = in.Close() - check(err) - bytes, err := ioutil.ReadAll(out) - check(err) - err = cmd.Wait() - check(err) - return string(bytes) -} - -// We'll break the code into {docs, code} pairs, and -// then render those text segments before including them -// in the HTML doc. -type seg struct { - docs, code, docsRendered, codeRendered string -} - -func main() { - // Accept exactly 1 argument - the input filename, - // less the .go extension. - if len(os.Args) != 2 { - fmt.Fprintln(os.Stderr, "unexpected args") - os.Exit(1) - } - - // Ensure that we have `markdown` and `pygmentize`, - // binaries, remember their paths. - markdownPath, err := exec.LookPath("markdown") - check(err) - pygmentizePath, err := exec.LookPath("pygmentize") - check(err) - - // Read the source file in, split into lines. - srcBytes, err := ioutil.ReadFile(os.Args[1]+".go") - check(err) - lines := strings.Split(string(srcBytes), "\n") - - // Group lines into docs/code segments. First, - // special case the header to go in its own segment. - headerDoc := docsPat.ReplaceAllString(lines[0], "") - segs := []*seg{} - segs = append(segs, &seg{code: "", docs: headerDoc}) - - // Then handle the remaining as code/doc pairs. - segs = append(segs, &seg{code: "", docs: ""}) - last := "" - for _, line := range lines[2:] { - head := segs[len(segs)-1] - docsMatch := docsPat.MatchString(line) - emptyMatch := line == "" - lastDocs := last == "docs" - newDocs := (last == "code") && head.docs != "" - newCode := (last == "docs") && head.code != "" - // Docs line - strip out comment indicator. - if docsMatch || (emptyMatch && lastDocs) { - trimed := docsPat.ReplaceAllString(line, "") - if newDocs { - newSeg := seg{docs: trimed, code: ""} - segs = append(segs, &newSeg) - } else { - head.docs = head.docs + "\n" + trimed - } - last = "docs" - // Code line - preserve all whitespace. - } else { - if newCode { - newSeg := seg{docs: "", code: line} - segs = append(segs, &newSeg) - } else { - head.code = head.code + "\n" + line - } - last = "code" - } - } - - // Render docs via `markdown` and code via - // `pygmentize` in each segment. - for _, seg := range segs { - seg.docsRendered = pipe( - markdownPath, - []string{}, - seg.docs) - seg.codeRendered = pipe( - pygmentizePath, - []string{"-l", "go", "-f", "html"}, - seg.code+" ") - } - - // Print HTML header. - title := titlePat.ReplaceAllString(lines[0], "") - fmt.Printf(` - - - - - %s - - - -
-
- - - - - - - - `, title) - - // Print HTML docs/code segments. - for _, seg := range segs { - fmt.Printf( - ` - - - `, seg.docsRendered, seg.codeRendered) - } - - // Print HTML footer. - fmt.Print(` -
%s%s
-
- - `) -} diff --git a/tool/gofmt b/tool/gofmt index 2c83ba2..c66a42c 100755 --- a/tool/gofmt +++ b/tool/gofmt @@ -1,3 +1,3 @@ #!/bin/bash -ls */*.go | xargs -n 1 gofmt -tabs=false -tabwidth=4 -w=true +ls src/*/*.go | xargs -n 1 gofmt -tabs=false -tabwidth=4 -w=true