-
-
Save taterbase/2589649 to your computer and use it in GitHub Desktop.
Revisions
-
beatgammit revised this gist
May 3, 2012 . 3 changed files with 38 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ package main import "fmt" type NotBool struct { i interface{} } func (e *NotBool) Error() string { return fmt.Sprintf("Expected bool, but found: %T", e.i) } func NewNotBoolError(i interface{}) *NotBool { err := new(NotBool) err.i = i return err } func assertBool(i interface{}) (bool, error) { if b, ok:= i.(bool); ok { return b, nil } return false, NewNotBoolError(i) } func main() { if b, err := assertBool(5); err != nil { fmt.Println(err) } else { fmt.Println("It was a bool:", b) } if b, err := assertBool(true); err != nil { fmt.Println(err) } else { fmt.Println("It was a bool:", b) } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ package main import( This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ package main import "fmt" -
beatgammit created this gist
May 3, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ package main import "fmt" type Appender struct { data []byte } func (a *Appender) Write(p []byte) (int, error) { newArr := append(a.data, p...) a.data = newArr return len(p), nil } func main() { appender := new(Appender) appender.data = []byte{} fmt.Fprintf(appender, "Hello %s", "World") fmt.Fprintf(appender, "\nI just added %d %s", 2, "lines") fmt.Println(string(appender.data)) } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ package main import( "fmt" "time" "math/rand" "os" "strconv" ) // current number (shared between goroutines) var num int = 0 // number of parts to split the computation into const PARTS int = 10 func fillRandom(arr []int, c chan int, n int) { for i, _ := range(arr) { arr[i] = num num++ // random sleep between 0-100 milliseconds dur := rand.Int63() % 100 time.Sleep(time.Duration(dur) * time.Millisecond) } // write to the channel to signify that we're done c <- n } func goListen(c chan int, done chan bool) { rets := 0 for rets < PARTS { fmt.Printf("Channel %d done\n", <-c) rets++ } done <- true } func main() { // grab the seed from the command-line, if provided seed = 500 if len(os.Args) > 1 { if seed, err := strconv.ParseInt(os.Args[1], 10, 64); err != nil { seed = 500 } } rand.Seed(int64(seed)) // make the array we're going to fill arr := make([]int, 100) // make a buffered channel c := make(chan int, PARTS) // the 'done' channel d := make(chan bool) go goListen(c, d) // slice up the array incr := len(arr) / PARTS for i := 0; i < PARTS; i++ { // create a slice for each segment slice := arr[i*incr : (i+1)*incr] // run each computation in it's own goroutine go fillRandom(slice, c, i) } // block until we're done <-d fmt.Println("All done!") fmt.Println(arr) } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ package main import "fmt" func main() { defer fmt.Println(" World") fmt.Println("Hello") } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ package main import "fmt" func sum(num ...int) (ret int) { for _, v := range(num) { ret += v } return ret } func main() { // call with known number of args fmt.Println(sum(5, 6, 7)) // call with unknown number of args arr := []int{5, 6, 7} fmt.Println(sum(arr...)) // call with unknown number of args, one line fmt.Println(sum([]int{5, 6, 7}...)) }