Skip to content

Instantly share code, notes, and snippets.

@taterbase
Forked from beatgammit/appender.go
Created May 3, 2012 21:25
Show Gist options
  • Select an option

  • Save taterbase/2589649 to your computer and use it in GitHub Desktop.

Select an option

Save taterbase/2589649 to your computer and use it in GitHub Desktop.

Revisions

  1. @beatgammit beatgammit revised this gist May 3, 2012. 3 changed files with 38 additions and 2 deletions.
    38 changes: 38 additions & 0 deletions assert.go
    Original 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)
    }
    }
    1 change: 0 additions & 1 deletion concurrent.go
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    package main

    import(
    1 change: 0 additions & 1 deletion variadic.go
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    package main

    import "fmt"
  2. @beatgammit beatgammit created this gist May 3, 2012.
    23 changes: 23 additions & 0 deletions appender.go
    Original 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))
    }
    78 changes: 78 additions & 0 deletions concurrent.go
    Original 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)
    }
    9 changes: 9 additions & 0 deletions defer.go
    Original 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")
    }
    24 changes: 24 additions & 0 deletions variadic.go
    Original 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}...))
    }