% title: Workshop GO % subtitle: Workshop GO % author: Emile Vauge % thankyou: Thank you! % contact: @emilevauge
title: Setup
- On GitHub, fork https://github.com/emilevauge/go-lyra
- Go to http://c9.io
- Sign in using your GitHub account
- In Cloud9, edit this repository
- Let's code !
title: GO in few words
- designed by Google (Robert Griesemer, Rob Pike, and Ken Thompson)
- open source
- commonly called golang
- simple, productive, readable
- a modern take on C
title: more words...
- fast (compile, run)
- garbage collected
- strongly typed
- statically linked binaries
title: Where is the catch?
- still early (1.0 in March 2012)
- dependencies management
- no generics (yet)
- not really OOP
- lack of powerful IDE
title: Here and there
- Google (obviously)
- Docker
- AirBnB
- SoundCloud
- Netflix
title: Tools
go build
: which builds Go binariesgo test
: for unit testing and microbenchmarksgo fmt
: for formatting codego run
: a shortcut for building and executing codegodoc
: for displaying documentation or serving it via HTTPgo generate
: a standard way to invoke code generators
title: Hello, Playground!
package main import "fmt" func main() { fmt.Println("Hello, playground") }
title: Main function
- Every go program has a main() function
- This is where execution starts
package main func main() { // Your program starts here }
title: Output
- fmt is a core package (means “format”)
- pronounced "fumpt" (weird)
package main import "fmt" func main() { fmt.Println("Hello Lyra!") }
title: Exercise 1_output
title: Variables
- Define
var myVar string
- Initialize
var myVar string
title: Type Inference
- Define and Initialize
var myVar string
- Initialize
myVar := "Hello"
title: Functions
func withArgs(a int, b string) int { return 3 } result := withArgs(3, "Hello")
func withMultipleReturns() (string, error) { return "Hello", nil } s, err := withMultipleReturns()
title: Exercise 4_returns
title: Structs subtitle: Define
type Person struct { Name string Age int } p := Person{ Name: "Greg", Age: 25, } fmt.Println(p.Name)
title: Structs subtitle: Access
- public Properties Are Uppercase
accessible to anything
- private properties are lowercase
only accessible via methods
title: Structs subtitle: Instantiation
type Person struct { Name string Age int } p := Person{ Name: "Greg", Age: 25, } fmt.Println(p.Name)
title: Structs subtitle: Methods
type Person struct { Name string Age int } func (p *Person) CanEnterPub() bool { return p.Age >= 19 } person.CanEnterPub()
title: Exercise 6_methods
title: GoRoutines
Goroutines are lightweight threads of execution
- executed concurrently
- supports > 100,000 goroutines
- automatically distributes load across CPUs
- Worry free!
title: GoRoutines subtitle: How?
doSomething()
go doSomething()
title: Channels
Goroutines communicate via channels: one-way pipes Operations:
- Push to channel
- Listen to channel
Channels have types too:
- var chan int
- var chan string
- var chan map[string]MyStruct
Use channels to share state, for control flow
title: Channels subtitle: Pushing
- Use
make()
to create a channel
var channel := make(chan int)
- Push to a channel with
<-
channel <- 5 channel <- 3
title: Channels subtitle: Listening
- Use select to wait (block) for a single message
- Similar syntax to case
select { case i <- channel: fmt.Printf("Got message %d", i) }
- Watch for multiple messages using range
for i := range channel { fmt.Printf("Got message %d", i) }
title: Exercise 9_channels
title: Loops
// for loop for i := 0; i < 10; i++ {...} // while loop for i < 10 {...} // infinite loop for {...}
title: Web
package main import ( "fmt" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) }
title: Exercise 12_web
title: Gorilla subtitle: Advanced routing
- URL host
- path
- path prefix
- schemes
- header
- query values
- HTTP methods
- custom matchers
title: Gorilla subtitle: Example
func main() { r := mux.NewRouter() r.HandleFunc("/", HomeHandler) r.HandleFunc("/products", ProductsHandler) r.HandleFunc("/articles", ArticlesHandler) http.ListenAndServe(":8080", r) }
title: Gorilla subtitle: Variables
r := mux.NewRouter() r.HandleFunc("/products/{key}", ProductHandler) r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler) r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
vars := mux.Vars(request) category := vars["category"]
title: Gorilla subtitle: Domain, Dynamic sub-domain
r := mux.NewRouter() // Only matches if domain is "www.example.com". r.Host("www.example.com") // Matches a dynamic subdomain. r.Host("{subdomain:[a-z]+}.domain.com")
title: Exercise 13_rest