On Github jhulten / presentation-learning-golang
Jeffrey Hulten
Engineering Manager
Developer Tools
Whitepages, Inc.
Assuming you know something like Python, Java, Ruby...
Using Packages Types, Structs, and Interfaces The Goroutine ChannelsGoogle for "golang", not "go".http://lmgtfy.com/?q=golang
Rich Hickey - Creator of Clojure
http://www.infoq.com/presentations/Simple-Made-Easy We program with constructs[...] but we are in a business of artifacts.Go is a language for concurrency
package main import "fmt" func main() { fmt.Println("Hello, playground") }
package main import ( "fmt" "code.google.com/p/gcfg" ) type Config struct { Section struct { Name string Flag bool } } func main() { var cfg Config err := gcfg.ReadFileInto(&cfg, "myconfig.gcfg") fmt.Println("The name is", cfg.Name) }
package mypkg func thisIsPrivate() {} func ThisIsPublic() {}
Golang is statically typed
var I int var myString string = "my string" fileReader, err := os.Open("/tmp/filename")http://golang.org/ref/spec#Variable_declarations
type TinyStruct struct { IsTiny bool } t1 := TinyStruct{} t2 := TinyStruct{true} t3 := TinyStruct{IsTiny: false} t3.IsTinyhttp://golang.org/ref/spec#Struct_types
type MyStruct struct { TinyStruct A string b uint32 Nested struct { C io.Reader } } var ms MyStruct ms = MyStruct{} ms.A = "one" ms.IsTiny = true if ms.TinyStruct.IsTiny {...}
type OtherStruct struct { MStruct MyStruct OtherField string } os := OtherStruct{MStruct : MyStruct{IsTiny: false}, OtherField : "thing"}
func FunctionName(argument string, arg1, arg2 int64) {...} func FunctionName(values ...int) error {...} func FunctionName()(namedReturn int) {...} var f := func() {...}http://golang.org/ref/spec#Function_declarations
type Thinger interface { DoThing() error } func (ms MyStruct)DoThing() error { return nil } func (os *OtherStruct)DoThing() error { return nil } func ProcessThinger(t Thinger) error { return t.DoThing() }http://golang.org/ref/spec#Interface_typeshttp://golang.org/ref/spec#Method_declarations
More Information: Rob Pike on Concurrency vs Parallelismhttp://vimeo.com/49718712
Pike is one of the three co-creators of GoLanggo time.Sleep(100 * time.Hour)
f := func() {time.Sleep(100 * time.Hour)} go f()
go func() {time.Sleep(100 * time.Hours())}()
var ch chan int ch = make(chan int)
ch := make(chan int, 10)
ch := make(chan int, 1) ch <- 1 ch <- 2
c := <-ch
ch := make(chan int, 10) ch <- 1 ch <- 2 ch <- 3 ch <- 4 for n := range ch { fmt.Println(n * n) }
ch := make(chan int, 10) done := make(chan bool) ch <- 1 ch <- 2 done <- true ch <- 4 select { case <-done: return case n <- ch: fmt.Println(n * n) }
func generate(nums ...int) <-chan int { out := make(chan int) go func() { for _, n := range nums { out <- n } close(out) }() return out }
func sq(in <-chan int) <-chan int { out := make(chan int) go func() { for n := range in { out <- n * n } close(out) }() return out }
func square(in <-chan int) <-chan int {} func generate(nums ...int) <-chan int {} func main() { for n := range square( square( generate( 2, 3, 4 ))) { fmt.Println(n) } }
go clean [packages] go clean github.com/goraft/raft
go get [-u] [packages] go get code.google.com/p/gcfg go get -u github.com/goraft/raft
go fmt [-n] [-x] [packages]
go build [-o output] [build flags] [packages] go build ./...
go test [-c] [-i] [build/test flags] [packages] [flags for test binary] go test ./... go test github.com/goraft/raft
ok archive/tar 0.011s FAIL archive/zip 0.022s ok compress/gzip 0.033s ...
go install [build flags] [packages] go install github.com/crosbymichael/skydock
jhulten@whitepages.com
twitter: @jhulten