Skip to content

Instantly share code, notes, and snippets.

@taterbase
Forked from beatgammit/appender.go
Created May 3, 2012 21:25
Show Gist options
  • Save taterbase/2589649 to your computer and use it in GitHub Desktop.
Save taterbase/2589649 to your computer and use it in GitHub Desktop.
UTOS- Introduction to Go
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))
}
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)
}
}
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)
}
package main
import "fmt"
func main() {
defer fmt.Println(" World")
fmt.Println("Hello")
}
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}...))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment