diff --git a/0-README.slide b/0-README.slide new file mode 100644 index 0000000..2186dde --- /dev/null +++ b/0-README.slide @@ -0,0 +1,18 @@ +README + + + +Vincent Batts +@vbatts +vbatts@hashbangbash.com + +* Overview + - This is a presentation tool from the golang team. + - Slide deck is a single flate file (*.slide) +.link https://godoc.org/golang.org/x/tools/present Present + + +* Slides + +.link https://github.com/vbatts/talks vbatts' talks + diff --git a/2013/03-golang-learning-lunch/1-hello/main.go b/2013/03-golang-learning-lunch/1-hello/main.go new file mode 100644 index 0000000..413665a --- /dev/null +++ b/2013/03-golang-learning-lunch/1-hello/main.go @@ -0,0 +1,8 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello World!") +} + diff --git a/2013/03-golang-learning-lunch/10-abstracts/main.go b/2013/03-golang-learning-lunch/10-abstracts/main.go new file mode 100644 index 0000000..5ec464c --- /dev/null +++ b/2013/03-golang-learning-lunch/10-abstracts/main.go @@ -0,0 +1,39 @@ +package main + +func main() { + jeep := Hummer{} + h(jeep) + jeep.Where() + c(jeep) +} + + +type Car interface { + Honk() + Crank() +} + +func c(some interface{}) { + some.(Car).Crank() +} +func h(some interface{}) { + some.(Car).Honk() +} + +// Implement the Car interface +type Hummer struct { + Car +} + +func (h Hummer) Honk() { + println("BEEP BEEP") +} + +func (h Hummer) Where() { + println("Who's got the keys to the jeep?") +} + +func (h Hummer) Crank() { + println("VROOOOOOM") +} + diff --git a/2013/03-golang-learning-lunch/11-defer/main.go b/2013/03-golang-learning-lunch/11-defer/main.go new file mode 100644 index 0000000..fe04d0a --- /dev/null +++ b/2013/03-golang-learning-lunch/11-defer/main.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +func main() { + // START OMIT + for i := 0; i < 5; i++ { + defer fmt.Printf("%d ", i) + } + fmt.Println("HOOTY HOO!") + // STOP OMIT +} diff --git a/2013/03-golang-learning-lunch/12-go-func/main.go b/2013/03-golang-learning-lunch/12-go-func/main.go new file mode 100644 index 0000000..4a66244 --- /dev/null +++ b/2013/03-golang-learning-lunch/12-go-func/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "time" +) + +// START OMIT +func DelayedHello() { + time.Sleep(3 * time.Second) + fmt.Printf("%d: Hello!\n", time.Now().Unix()) +} + +func main() { + go DelayedHello() + go DelayedHello() + go func() { + time.Sleep(3 * time.Second) + fmt.Printf("%d: Hello!\n", time.Now().Unix()) + }() + + fmt.Printf("%d: Is there anybody in there?!\n", time.Now().Unix()) + + for i := 0; i < 5; i++ { + fmt.Printf("%d: .\n", time.Now().Unix()) + time.Sleep(time.Second) + } +} +// STOP OMIT + diff --git a/2013/03-golang-learning-lunch/13-blank-var/main.go b/2013/03-golang-learning-lunch/13-blank-var/main.go new file mode 100644 index 0000000..7b6d367 --- /dev/null +++ b/2013/03-golang-learning-lunch/13-blank-var/main.go @@ -0,0 +1,14 @@ +package main + +func main() { + // START OMIT + words := []string{"cow","goat","sheep","horse","chicken"} + for i := range words { + println(words[i]) + } + for _, word := range words { + println(word) + } + // STOP OMIT +} + diff --git a/2013/03-golang-learning-lunch/14-ngoroutines/main.go b/2013/03-golang-learning-lunch/14-ngoroutines/main.go new file mode 100644 index 0000000..0d7d319 --- /dev/null +++ b/2013/03-golang-learning-lunch/14-ngoroutines/main.go @@ -0,0 +1,20 @@ +package main + +// START OMIT +var ngoroutine = 100000 + +func f(left, right chan int) { left <- 1 + <-right } + +func main() { + leftmost := make(chan int) + var left, right chan int = nil, leftmost + for i := 0; i < ngoroutine; i++ { + left, right = right, make(chan int) + go f(left, right) + } + right <- 0 // bang! + x := <-leftmost // wait for completion + println(x) // 100000 +} + +// STOP OMIT diff --git a/2013/03-golang-learning-lunch/2-hello/main.go b/2013/03-golang-learning-lunch/2-hello/main.go new file mode 100644 index 0000000..ec102fa --- /dev/null +++ b/2013/03-golang-learning-lunch/2-hello/main.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func main() { + var words string + + words = "Hello World!" + + for i := 0; i < 10; i++ { + fmt.Printf("%s\n", words) + } +} diff --git a/2013/03-golang-learning-lunch/2013-03-27-go-ll.slide b/2013/03-golang-learning-lunch/2013-03-27-go-ll.slide new file mode 100644 index 0000000..635892a --- /dev/null +++ b/2013/03-golang-learning-lunch/2013-03-27-go-ll.slide @@ -0,0 +1,350 @@ +Golang: an introduction + +27 Mar 2013 + +# Go is a general-purpose language that bridges the gap between efficient +# statically typed languages and productive dynamic language. But it’s not just +# the language that makes Go special – Go has broad and consistent standard +# libraries and powerful but simple tools. +# +# This talk gives an introduction to Go + +# Please join Vincent Batts, Senior Software Application Engineer in the IT-Eng tower, for an overview of the Go programming language. +# +# Audience: Hackers, doodlers, early adopters, linguists +# Agenda: +# * Overview of just what the language is and why should anyone care? +# * What makes it better or worse than other options. +# * Is it production ready?! +# * Common go-idioms +# * Other questions folks have relevant to Red Hat IT ? +# +# Background: +# The Go programming language is an open source project to make programmers more productive. +# Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language. + +Vincent Batts +Red Hat, Inc. +@vbatts +vbatts@redhat.com +http://youtu.be/bIs15Km9v4k + +* Google Motivation +- Efficiency +- Safety +- Concurrency +- Scalabilty +- Fast Dev Cycle +- No Surprises +- ... Such a cute mascot + +* gophers +# SO DARN CUTE +.image 2013-03-27-go-ll/gopher.jpg + +* Benefits +- Spec driven + +.link http://golang.org/ref/spec + +- Changes must have consenus +# agreement of the different world views + +* Hello, go + +.play 1-hello/main.go + +* Layout Overview + +- packages (namespaces) +# no need for central trust model +# not terribly unlike python +- remote packages include host/path +# we'll touch back on this on the go tool +- packages hold types, constants, variables, structs and func's +- packages are grouped by directory (all *.go siblings are grouped) +# we'll get back to testing +- Capitalization determines visibility: `Foo` is exported, `foo` is not + +* Tooling + +* Compilers +- gc - Google Go Compiler + - f19+ 'golang' + - EPEL soon... +# either from source or there is an RPM build +# +# COMPILES SO FAST +# +# 100k lines of code compiles in 2min +# (the equivalent C++ took greater than 1hr) +# +- gccgo - GCC 4.7.1 and newer + - f16+ +# fedora 16+ and RHEL7 + +* go tool +the defacto all-the-things tool +# wraps up testing, building, docs and libraries + +Compile a single-file program + + $ go build hello.go + +Compile and run a single-file program + + $ go run hello.go + +List libraries available + + $ go list all + +See documentation + + $ go doc io/ioutil + +* go tool + +Cleanup code + + $ go fmt main.go + +Test code + + $ go test main_test.go + +Check code for suspicious constructs or inconsistencies + + $ go vet main.go + +* go tool + +Compile out the current directory's *.go + + $ cd my-project/ + $ go build + $ ./my-project + +Test the current directory's *.go + + $ cd my-project/ + $ go test + +* Standard libraries +# are freaking _solid_ +# +# Very well documented +# +# and easy to read + +* basic types + +.link http://golang.org/ref/spec#Types + +- Familiar C-types +- But Strings too! +# common and comfortable enough, without having to think of strings +# as *char. We have []byte for that now +- Slices - static and dynamic arrays + +* structs and pointers + + type Person struct { + Name string + Dob *time.Time + } + +.play ./3-struct/main.go /START/,/STOP/ + +* structs and pointers + + func (p *Person) ComeToLife() { + t := time.Now() + p.Dob = &t + p.State = NEW_BORN + } + +.play ./4-struct-func/main.go /START/,/STOP/ + +* Why should I care? +# Perhaps you shouldn't care, if you ask this ... :-) + +Why Another Language? +# Surprisingly, much of the concepts of Golang are _not_ "new" +# concepts were taken from Ada, and heavily succeeds from plan9 and erlang +# (ken thompson is one of the core developers) +# +# like so many businesses they have a need for business logic/ performance +# but also the speed and ease of getting new features/apps deployed +# +# This often splits them over two languages. In google's situation, the need +# for a single language came from their heavy use of C and Python. +# +# Compile times! none of the ifdef garb, +# and no shadiness of pre-proccessor, header and linker issues + +Benefits for me? +# That's a big question that would involve a good deal of review +# +# reduced code. logic becomes visible + +Benefits for Red Hat IT? +# Even tougher to say. +# Definitely has benefits for portability, light-stack, speed and stability +# No conversations should be considered, but also should not be ruled out +# for future projects. +# +# Ops/Infra burden. No stack, just land the binary. +# Server needs no additional software, just a bare bones linux box +# +# StatHat +# ruby -> go services find 10x performance increase +# +# IronWorker +# 30 ruby servers -> 2 go servers (just for redundancy) +# + +* Is this production ready?! + +Great question! + +.link https://code.google.com/p/go-wiki/wiki/SuccessStories + +Google uses it +- dl.google.com +- part of Youtube + +.link https://code.google.com/p/vitess/ Youtube - vitess project + + +* Idiomatic + +* Very +- expressive +- predictable +- parsable (gofmt) +- what-you-see-is-what-you-get +# not a bunch of hidden inheritance +# or prototyping +# or code sprawl + + +* Idioms + +- Multiple return values +- Error checking (rather than exception handling) + + fn := "/tmp/this-file.txt" + fh, err := os.Open(fn) + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: %s failed cause: %s!\n", fn, err) + // ... check for type of error, and do something + } + // ... life goes on + + +* Idioms + +anonymous functions + +.link http://golang.org/doc/effective_go.html#goroutines go routines + +.play 12-go-func/main.go /START/,/STOP/ + + +* Idioms + +.link http://golang.org/doc/effective_go.html#defer Defer +# closing a handle, unlocking a mutex, some stack of operations, etc. + +.play 11-defer/main.go /START/,/STOP/ + +classic file handle situation... made simple + + fh, err := os.Open("foo.txt") + if err != nil { + return "", err + } + defer fh.Close() + // forget about needing to close the file handle + + +* Idioms + +Only what you need + +- import's +# failed compile for including libraries that aren't used +- variables + +.play 13-blank-var/main.go /START/,/STOP/ + + +* Idioms + +.link http://golang.org/doc/effective_go.html#channels Channels +# nice closures + + c := make(chan int) // Allocate a channel. + // Start the sort in a goroutine; when it completes, signal on the channel. + go func() { + list.Sort() + c <- 1 // Send a signal; value does not matter. + }() + doSomethingForAWhile() + <-c // Wait for sort to finish; discard sent value. + + +* channels +- using it for signal catching + + go func() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + signal.Notify(c, os.Kill) + for sig := range c { + // sig is a ^C, handle it + if sig == os.Interrupt { + log.Println("interrupted ...") + os.Exit(1) + } else if sig == os.Kill { + log.Println("killing ...") + os.Exit(2) + } else { + log.Printf("Not sure what %q is. Quiting", sig) + os.Exit(3) + } + } + }() + + +* concurrency + +.play 14-ngoroutines/main.go /START/,/STOP/ + +* Straight Forward + +.link http://localhost:4000/ Web Server + +.play ./9-http-srv/main.go /START/,/STOP/ + +* Other Questions?? + + +* References + +.link http://golang.org/doc/ + +.link http://golang.org/pkg/ + +.link http://godoc.org/ + +freenode #go-nuts + +.link http://groups.google.com/group/golang-nuts + +.link http://youtu.be/sln-gJaURzk Google I/O 2012 - Meet the Go Team + +.link http://www.miek.nl/projects/learninggo/ Learning Go book (free) + + diff --git a/2013/03-golang-learning-lunch/2013-03-27-go-ll/gopher.jpg b/2013/03-golang-learning-lunch/2013-03-27-go-ll/gopher.jpg new file mode 100644 index 0000000..0e886e4 Binary files /dev/null and b/2013/03-golang-learning-lunch/2013-03-27-go-ll/gopher.jpg differ diff --git a/2013/03-golang-learning-lunch/3-struct/main.go b/2013/03-golang-learning-lunch/3-struct/main.go new file mode 100644 index 0000000..d8c9ec1 --- /dev/null +++ b/2013/03-golang-learning-lunch/3-struct/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "time" +) + +type Person struct { + Name string + Dob *time.Time +} + +func main() { + // START OMIT + t, err := time.Parse(time.RFC822, "27 Mar 75 00:00 EST") + if (err != nil) { + fmt.Println(err) + return + } + p := Person{Name: "John Doe", Dob: &t } + fmt.Printf("%q\n", p) + // STOP OMIT + + //fmt.Printf("%s\n", p.Name) + //fmt.Printf("%s\n", p.Dob.String()) +} + diff --git a/2013/03-golang-learning-lunch/4-struct-func/main.go b/2013/03-golang-learning-lunch/4-struct-func/main.go new file mode 100644 index 0000000..3c1eaf1 --- /dev/null +++ b/2013/03-golang-learning-lunch/4-struct-func/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "time" +) + +const ( + IN_UTERO = iota + NEW_BORN + GROWING + LIVING + DYING + DEAD +) + +type Person struct { + Name string + Dob *time.Time + State byte +} + +func (p *Person) DobString() string { + if (p.Dob == nil) { + return "" + } + return p.Dob.String() +} + +func (p *Person) ComeToLife() { + t := time.Now() + p.Dob = &t + p.State = NEW_BORN +} + + +func main() { + // START OMIT + p := Person{Name: "John Doe", State: IN_UTERO} + fmt.Printf("%s, %s\n", p.Name, p.DobString()) + + p.ComeToLife() + fmt.Printf("%s, %s\n", p.Name, p.DobString()) + // STOP OMIT +} + diff --git a/2013/03-golang-learning-lunch/5-neats/main.go b/2013/03-golang-learning-lunch/5-neats/main.go new file mode 100644 index 0000000..674849f --- /dev/null +++ b/2013/03-golang-learning-lunch/5-neats/main.go @@ -0,0 +1,24 @@ +package main + +import "fmt" + +func main() { + numbers := []int{1,7,3,8,2,4,5,4,9,0} + + for i := range numbers { + fmt.Printf("index: %d\n", i) + } + + for _, v := range numbers { + fmt.Printf("value: %d\n", v) + } + + people := map[string]Person{} + people["shawking"] = Person{ Name: "Stephen Hawking", State: LIVING} + people["dritchie"] = Person{ Name: "Dennis Ritchie", State: DEAD} + for k,v := range people { + fmt.Printf("Key: %s; Object: %q\n", k, v) + } + +} + diff --git a/2013/03-golang-learning-lunch/5-neats/person.go b/2013/03-golang-learning-lunch/5-neats/person.go new file mode 100644 index 0000000..ec83644 --- /dev/null +++ b/2013/03-golang-learning-lunch/5-neats/person.go @@ -0,0 +1,34 @@ +package main + +import ( + "time" +) + +const ( + IN_UTERO = iota + NEW_BORN + GROWING + LIVING + DYING + DEAD +) + +type Person struct { + Name string + Dob *time.Time + State byte +} + +func (p *Person) DobString() string { + if (p.Dob == nil) { + return "" + } + return p.Dob.String() +} + +func (p *Person) ComeToLife() { + t := time.Now() + p.Dob = &t + p.State = NEW_BORN +} + diff --git a/2013/03-golang-learning-lunch/6-inheritance/main.go b/2013/03-golang-learning-lunch/6-inheritance/main.go new file mode 100644 index 0000000..391ad2e --- /dev/null +++ b/2013/03-golang-learning-lunch/6-inheritance/main.go @@ -0,0 +1,40 @@ + +package main + +import ( + "log" + "os" + "os/exec" + "time" +) + +type Job struct { + Command string + *log.Logger +} + +func NewJob(cmd string) *Job { + return &Job{ cmd, log.New(os.Stderr, "[Job] ", log.Ldate)} +} + +func (j *Job) Run() ([]byte, error) { + j.Printf("Running [%s] ...", j.Command) + out, err := exec.Command(j.Command).Output() + if (err != nil) { + j.Fatal(err) + return nil, err + } + j.Printf("Command returned [%s]", out) + return out, nil +} + +func main() { + job := NewJob("uptime") + job.Run() + + for i := 0; i < 10; i++ { + job.Run() + time.Sleep(1 * time.Second) + } +} + diff --git a/2013/03-golang-learning-lunch/7-prof/main.go b/2013/03-golang-learning-lunch/7-prof/main.go new file mode 100644 index 0000000..2a5c4bc --- /dev/null +++ b/2013/03-golang-learning-lunch/7-prof/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "log" + "net/http" + _ "net/http/pprof" + "time" +) + +func main() { + go func(){ log.Println(http.ListenAndServe("localhost:6060",nil))}() + log.Println("Sleeping") + for { + log.Print(".") + time.Sleep(3 * time.Second) + } +} + diff --git a/2013/03-golang-learning-lunch/8-io-chain/main.go b/2013/03-golang-learning-lunch/8-io-chain/main.go new file mode 100644 index 0000000..403b2cd --- /dev/null +++ b/2013/03-golang-learning-lunch/8-io-chain/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "compress/gzip" + "encoding/base64" + "io" + "os" + "strings" +) + +func main() { + var r io.Reader + r = strings.NewReader(data) + r = base64.NewDecoder(base64.StdEncoding, r) + r, _ = gzip.NewReader(r) + io.Copy(os.Stdout, r) +} + +const data = ` +H4sIAAAJbogA/1SOO5KDQAxE8zlFZ5tQXGCjjfYIjoURoPKgcY0E57f4VZlQXf2e+r8yOYbMZJhoZWRxz3wkCVjeReETS0VHz5fBCzpxxg/PbfrT/gacCjbjeiRNOChaVkA9RAdR8eVEw4vxa0Dcs3Fe2ZqowpeqG79L995l3VaMBUV/02OS+B6kMWikwG51c8n5GnEPr11F2/QJAAD//z9IppsHAQAA +` + diff --git a/2013/03-golang-learning-lunch/9-http-srv/main.go b/2013/03-golang-learning-lunch/9-http-srv/main.go new file mode 100644 index 0000000..cc8fbbf --- /dev/null +++ b/2013/03-golang-learning-lunch/9-http-srv/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "log" + "net/http" +) + +// START OMIT +func routeSlash(w http.ResponseWriter, r *http.Request) { + log.Printf("Got a request from %s", r.RemoteAddr) + fmt.Fprintf(w, "It Works!\n") +} + +func main() { + log.Println("listening on 0.0.0.0:4000") + http.HandleFunc("/", routeSlash) + log.Fatal(http.ListenAndServe("0.0.0.0:4000", nil)) +} +// STOP OMIT + diff --git a/2016/12-intro-to-golang/README.md b/2016/12-intro-to-golang/README.md new file mode 100644 index 0000000..719f863 --- /dev/null +++ b/2016/12-intro-to-golang/README.md @@ -0,0 +1,6 @@ +Usage +==== + + go get golang.org/x/tools/cmd/present + present . + diff --git a/2016/12-intro-to-golang/cats20.gif b/2016/12-intro-to-golang/cats20.gif new file mode 100644 index 0000000..b6c353a Binary files /dev/null and b/2016/12-intro-to-golang/cats20.gif differ diff --git a/2016/12-intro-to-golang/good0.go b/2016/12-intro-to-golang/good0.go new file mode 100644 index 0000000..3a3b880 --- /dev/null +++ b/2016/12-intro-to-golang/good0.go @@ -0,0 +1,48 @@ +// +build ignore + +package main + +import ( + "compress/gzip" + "encoding/json" + "flag" + "io/ioutil" + "log" + "os" +) + +func main() { + for _, arg := range flag.Args() { + func() { + // START1 OMIT + fh, err := os.Open(arg) + if err != nil { + log.Fatal(err) + } + defer fh.Close() + + gz, err := gzip.NewReader(fh) + if err != nil { + log.Fatal(err) + } + defer gz.Close() + + buf, err := ioutil.ReadAll(gz) + if err != nil { + log.Fatal(err) + } + + var mine MyStruct + err = json.Unmarshal(&mine) + if err != nil { + log.Fatal(err) + } + // STOP1 OMIT + + }() + } + +} + +type MyStruct struct { +} diff --git a/2016/12-intro-to-golang/good1.go b/2016/12-intro-to-golang/good1.go new file mode 100644 index 0000000..84a3b6e --- /dev/null +++ b/2016/12-intro-to-golang/good1.go @@ -0,0 +1,20 @@ +// +build ignore + +package main + +// START1 OMIT +// exported +type Foo struct { + Bar string // exported + baz bool // private +} + +// private +type bif struct { + Harf, Buz int64 // doesn't matter +} + +// STOP1 OMIT + +func main() { +} diff --git a/2016/12-intro-to-golang/gopher.png b/2016/12-intro-to-golang/gopher.png new file mode 100644 index 0000000..cb5e2e1 Binary files /dev/null and b/2016/12-intro-to-golang/gopher.png differ diff --git a/2016/12-intro-to-golang/hello.go b/2016/12-intro-to-golang/hello.go new file mode 100644 index 0000000..8ad6d97 --- /dev/null +++ b/2016/12-intro-to-golang/hello.go @@ -0,0 +1,10 @@ +// +build ignore + +// START1 OMIT +package main + +func main() { + println("Howdy y'all") +} + +// STOP1 OMIT diff --git a/2016/12-intro-to-golang/imports.go b/2016/12-intro-to-golang/imports.go new file mode 100644 index 0000000..ba18875 --- /dev/null +++ b/2016/12-intro-to-golang/imports.go @@ -0,0 +1,18 @@ +// +build ignore + +package main + +// START1 OMIT +import ( + "fmt" + "os" + + "github.com/foo/bar" +) + +// STOP1 OMIT + +func main() { + fmt.Println(bar.Baz()) + _ = os.FileInfo +} diff --git a/2016/12-intro-to-golang/kanye_imma_bookmarklet.png b/2016/12-intro-to-golang/kanye_imma_bookmarklet.png new file mode 100644 index 0000000..e896826 Binary files /dev/null and b/2016/12-intro-to-golang/kanye_imma_bookmarklet.png differ diff --git a/2016/12-intro-to-golang/pingpong.go b/2016/12-intro-to-golang/pingpong.go new file mode 100644 index 0000000..159d2c5 --- /dev/null +++ b/2016/12-intro-to-golang/pingpong.go @@ -0,0 +1,33 @@ +// +build OMIT + +package main + +import ( + "fmt" + "time" +) + +// STARTMAIN1 OMIT +type Ball struct{ hits int } + +func main() { + table := make(chan *Ball) + go player("ping", table) + go player("pong", table) + + table <- new(Ball) // game on; toss the ball + time.Sleep(1 * time.Second) + <-table // game over; grab the ball +} + +func player(name string, table chan *Ball) { + for { + ball := <-table + ball.hits++ + fmt.Println(name, ball.hits) + time.Sleep(100 * time.Millisecond) + table <- ball + } +} + +// STOPMAIN1 OMIT diff --git a/2016/12-intro-to-golang/primitive1.go b/2016/12-intro-to-golang/primitive1.go new file mode 100644 index 0000000..d9e1961 --- /dev/null +++ b/2016/12-intro-to-golang/primitive1.go @@ -0,0 +1,25 @@ +// +build ignore + +package main + +import "fmt" + +func main() { + // START1 OMIT + names := []string{ + "Michael", + "Jan", + "Sean", + "Silvia", + } + for i, name := range names { + fmt.Printf("%q is the %d name\n", name, i) + } + for i := range names { + fmt.Printf("%q is the %d name\n", names[i], i) + } + for _, name := range names { + fmt.Printf("%q is the ... name\n", name) + } + // STOP1 OMIT +} diff --git a/2016/12-intro-to-golang/primitive2.go b/2016/12-intro-to-golang/primitive2.go new file mode 100644 index 0000000..9d54624 --- /dev/null +++ b/2016/12-intro-to-golang/primitive2.go @@ -0,0 +1,56 @@ +// +build ignore + +package main + +import "fmt" + +func main() { + // START1 OMIT + infos := map[string]Info{ + "Michael": Info{ + City: "Happyville", + Status: Open, + }, + "Jan": Info{ + City: "Confusville", + Status: Closed, + }, + "Sean": Info{ + City: "Septober", + Status: Complicated, + }, + "Silvia": Info{ + City: "Curiousville", + Status: Curios, + }, + } + for name, info := range infos { + fmt.Printf("%q is %s in %q\n", name, info.Status, info.City) + } + // STOP1 OMIT +} + +type Info struct { + City string + Status Status +} +type Status int + +var ( + Open = Status(0) + Closed = Status(1) + Complicated = Status(2) + Curios = Status(3) +) + +func (s Status) String() string { + switch s { + case Open: + return "open" + case Closed: + return "closed" + case Complicated: + return "complicated" + } + return "hurr" +} diff --git a/2016/12-intro-to-golang/primitive3.go b/2016/12-intro-to-golang/primitive3.go new file mode 100644 index 0000000..c3329d5 --- /dev/null +++ b/2016/12-intro-to-golang/primitive3.go @@ -0,0 +1,37 @@ +// +build ignore + +package main + +import "fmt" + +func main() { + // START1 OMIT + fmt.Println("I've got:") + for card := range ReadEmAndWeep() { + fmt.Printf(" %s of %s\n", card.Value, card.Suite) + } + // STOP1 OMIT +} + +type Card struct { + Suite, Value string +} + +// START2 OMIT +func ReadEmAndWeep() <-chan Card { + c := make(chan Card) + go func() { + for _, card := range []Card{ + Card{"Ace", "Jack"}, + Card{"Ace", "Queen"}, + Card{"Ace", "King"}, + Card{"Ace", "Ace"}, + } { + c <- card + } + close(c) + }() + return c +} + +// STOP2 OMIT diff --git a/2016/12-intro-to-golang/rainbow.jpg b/2016/12-intro-to-golang/rainbow.jpg new file mode 100644 index 0000000..eb3c98e Binary files /dev/null and b/2016/12-intro-to-golang/rainbow.jpg differ diff --git a/2016/12-intro-to-golang/revenge-of-the-nerds-o.gif b/2016/12-intro-to-golang/revenge-of-the-nerds-o.gif new file mode 100644 index 0000000..fce49b6 Binary files /dev/null and b/2016/12-intro-to-golang/revenge-of-the-nerds-o.gif differ diff --git a/2016/12-intro-to-golang/tags.go b/2016/12-intro-to-golang/tags.go new file mode 100644 index 0000000..6e89e71 --- /dev/null +++ b/2016/12-intro-to-golang/tags.go @@ -0,0 +1,8 @@ +// START1 OMIT +// +build !windows linux,cgo +// STOP1 OMIT + +package main + +func main() { +} diff --git a/2016/12-intro-to-golang/talk.slide b/2016/12-intro-to-golang/talk.slide new file mode 100644 index 0000000..ff0db2d --- /dev/null +++ b/2016/12-intro-to-golang/talk.slide @@ -0,0 +1,262 @@ +Intro to Golang +05 December 2016 + +Vincent Batts +Developer +@vbatts +vbatts@redhat.com +https://github.com/vbatts/talks + +* howdy + + $> finger $(whoami) + Login: vbatts Name: Vincent Batts + Such mail. + Plan: + right and joyful effort + $> id -Gn + devel redhat oci containers openshift slackware docker + +: Surview folks familiarity with golang. And with their primary language. + +* golang + +: Notes +: - libraries - source only, but can link to C *.so and *.a +: - Fork/Exec are coupled together (for coroutine and GC reasons) +: - Threading, and multiproc, concurrent logic +: -- nice and easy to use +: -- Make for tricky handling of C calls (i.e. setns) +: - `go get` is handy +: - cross-compile without hardly any bootstrapping +: -- native compiler supported arches +: -- gccgo works for the arch gcc is compiled for +: - primitives can seem a bit magical +: -- conditional returns +: -- for ... range +: -- iota +: - no ifdefs, but build tags +: - error handling, rather than exception catching + + +* + +.image ./gopher.png + + +* Overview + +- strongly typed +- compiled +- stylistically nice +- opinionated/idiomatic + + +* + +.image ./rainbow.jpg + + +* fully qualified imports + +.code ./imports.go /START1/,/STOP1/ + + +* fast compiles + +(Perhaps other compilers are slow) + +.play ./hello.go /START1/,/STOP1/ + + +* defer + +.code -numbers ./good0.go /START1/,/STOP1/ + + +* Garbage Collected + +- Super convenient +- references +- completed goroutines + + +* Garbage Collected + +.link https://twitter.com/brianhatfield/status/634166123605331968 Brian Hatfield GC improvements + +- go1.4 (300ms) -> go1.5 (~30ms) +- go1.6.0 (25ms) -> go1.6.3 (5ms) +- go1.7.3 (3ms) -> go1.8beta1 (sub ms on 18Gb heap) + + +* simple exports + +.code ./good1.go /START1/,/STOP1/ + + +* concurrency + +.play -numbers ./pingpong.go /STARTMAIN1/,/STOPMAIN1/ +.link http://talks.golang.org/2013/advconc.slide Sameer Ajmani - Advanced Concurrency + + +* cross compiles + +: this could be demo'ed by scp'ing the binaries to fats.userys (aarch64) and piaba.usersys (arm v7) + +(staying away from CGO) + +.play ./hello.go /START1/,/STOP1/ + + $> go build ./main.go + $> file main + main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped + $> GOOS=windows GOARCH=386 go build ./main.go + $> file main.exe + main.exe: PE32 executable (console) Intel 80386 (stripped to external PDB), for MS Windows + $> GOOS=openbsd GOARCH=arm go build ./main.go + $> file main + main: ELF 32-bit LSB executable, ARM, version 1 (OpenBSD), statically linked, for OpenBSD, not stripped + $> GOARCH=arm64 go build ./main.go + $> file main + main: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, not stripped + + +* helpers + +- `go get golang.org/x/tools/cmd/godoc` +- `go get github.com/golang/lint/golint` +- https://github.com/fatih/vim-go + +- `go vet ./...` +- `go test ./...` +- `golint -set_exit_status ./...` + + +* +.image ./kanye_imma_bookmarklet.png 250 _ +.caption _kanye_ by Kayne + +- easy learning curve +- `go get` +- formatting wars are "over" +- shared libraries + + +* + +: they come along, and affect how you do your work +.image ./cats20.gif +.caption _halping_hooman_ by cat + + +* Packaging + +Addresses different concern than distributions + + +* lack of generics? + +: i don't feel strongly about this, though many do +- interfaces - are enough for most +- go1.4 introduced go:generate +: produce code for Set, Graph etc, for the types needed, but at compile time. No need to reflect. + + +* Debugging + +- gdb is there, sort of +- some known debugging tools for ELF are not useful +- fmt.Printf("%#v\n", ...) +: show break on main.main and fmt.Println +: continue, list, then step, then list, and then face melt + + +* Concurrency and CGO + +Calls like setns(2) are rough + +(yes, even with runtime.LockOSThread()) + +Embedding other languages (like ruby and python that have their green threading) + + +* Fork/Exec + +Not Separate, but together + + +* +.image ./revenge-of-the-nerds-o.gif +.caption _dancin'_ in ROTN + + +* build tags + +no #ifdef + +.code ./tags.go /START1/,/STOP1/ + +or files with *_linux.go like suffix. + +More like extern. + + +* _ + +bit bucket + + +* channels + +.play ./ugly0.go /START1/,/STOP1/ + + +* iota + +.code ./ugly1.go /START1/,/STOP1/ + + +* for range + +array (or string) + +.play ./primitive1.go /START1/,/STOP1/ + + +* for range + +map + +.play ./primitive2.go /START1/,/STOP1/ + + +* for range + +channel (like an iterator) + +.play ./primitive3.go /START1/,/STOP1/ + + +* for range + +channel + +.code ./primitive3.go /START2/,/STOP2/ + + +* Conclusions? + + +* use-case + +- like all languages, align with you use-case +- get familiar enough to like and dislike it +- don't be afraid to try it out + + +* References + +.link https://golang.org/doc/ Go Documentation +.link https://golang.org/doc/effective_go.html Effective Go diff --git a/2016/12-intro-to-golang/ugly0.go b/2016/12-intro-to-golang/ugly0.go new file mode 100644 index 0000000..715c384 --- /dev/null +++ b/2016/12-intro-to-golang/ugly0.go @@ -0,0 +1,16 @@ +// +build ignore + +package main + +func main() { + // START1 OMIT + c := make(chan int) + go func() { + for i := 0; i < 10; i++ { + c <- i + } + close(c) + }() + println(<-c) + // STOP1 OMIT +} diff --git a/2016/12-intro-to-golang/ugly1.go b/2016/12-intro-to-golang/ugly1.go new file mode 100644 index 0000000..3d72e71 --- /dev/null +++ b/2016/12-intro-to-golang/ugly1.go @@ -0,0 +1,22 @@ +// +build ignore + +package main + +// START1 OMIT +const ( + DeviceCreate TaskType = iota + DeviceReload + DeviceRemove + DeviceRemoveAll + DeviceSuspend + DeviceResume + DeviceInfo + DeviceDeps +) + +// STOP1 OMIT + +type TaskType int + +func main() { +}