diff --git a/index.html b/index.html index 3088160..3d869d2 100644 --- a/index.html +++ b/index.html @@ -12,7 +12,7 @@ - + @@ -24,10 +24,11 @@ .goplay { background-color: rgb(255, 255, 255); width: 900px; height: 550px; max-height: none; max-width: none; } .reveal dt { font-family:Inconsolata,Monospace; font-weight:900; text-align: left; float: left;} .reveal dd { font-weight:normal; text-align: right;} - .reveal code { font-family: Courier,Monospace; text-transform:none; letter-spacing: 0;} + .reveal code { font-family: Courier,Monospace; text-transform:none; letter-spacing: 0; } .reveal blockquote { box-shadow:none; } .reveal pre { box-shadow:none; } - .reveal section img { border:0px; box-shadow:none; } + .reveal pre code { max-height: 750px; font-size: 36px; line-height: normal; } + .reveal section img { border:0px; box-shadow:none; line-height: 120%; } @@ -52,8 +53,8 @@

Learning to Write Go

+

Jeffrey Hulten

-

Who Am I

@@ -77,131 +78,25 @@

What I'm Going to Cover

-

Top Three Hints

-
    -
  1. Google for "golang", not "go".
    - http://lmgtfy.com/?q=golang
    -
    -
  2. -
  3. Try the standard library first.
    Especially "net/http"
  4. -
  5. Learn to love channels
  6. -
+

Not For You? Parting Hint...

+

Google for "golang", not "go".
+ http://lmgtfy.com/?q=golang +

-

The Artifact is the Thing

- +

Simplicity

+

+ 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

- -
-
-

Common Toolchain, Common Practices

-
-
    -
  • go clean
  • -
  • go get
  • -
  • go fmt
  • -
  • go test
  • -
  • go build
  • -
-
-
- -
-

go clean

-

-go clean [-i] [-r] [-n] [-x] [packages]
-					
-
-
_obj/
old object directory, left from Makefiles
-
_test/
old test directory, left from Makefiles
-
_testmain.go
old gotest file, left from Makefiles
-
test.out
old test log, left from Makefiles
-
build.out
old test log, left from Makefiles
-
*.[568ao]
object files, left from Makefiles
- -
DIR(.exe)
from go build
-
DIR.test(.exe)
from go test -c
-
MAINFILE(.exe)
from go build MAINFILE.go
-
*.so
from SWIG
-
-
- -
-

go get

-
    -
  • Downloads and installs the packages named by the import paths, along with their dependencies.
  • -
-

-go get [-d] [-fix] [-t] [-u] [build flags] [packages]
-	
-
- - -
-

go fmt

-
    -
  • Reformats the packages listed to the golang standard.
  • -
  • It prints the names of the files that are modified.
  • -
  • Many editors run go fmt on save.
  • -
-

-go fmt [-n] [-x] [packages]
-	
-
- - -
-

go test

-
    -
  • Recompiles each package along with any files with names matching "*_test.go".
  • -
  • Can contain test functions, benchmark functions, and example functions.
  • -
  • It prints a summary of the test results in the format: -
    
    -ok   archive/tar   0.011s
    -FAIL archive/zip   0.022s
    -ok   compress/gzip 0.033s
    -...
    -
  • -
-

-go test [-c] [-i] [build/test flags] [packages] [flags for test binary]
-go test ./...
-go test github.com/goraft/raft
-	
-
- - -
-

go build

-
    -
  • Compiles the packages named by the import paths, along with their dependencies.
  • -
  • Does not install the results.
  • -
-

-go build [-o output] [build flags] [packages]
-go build ./...
-	
-
- -
-

go install

-
    -
  • Compiles and installs the packages named by the import paths, along with their dependencies
  • -
-

-go install [build flags] [packages]
-go install github.com/crosbymichael/skydock
-	
-
- - -
-

Hello, Playground! @@ -211,14 +106,11 @@

http://play.golang.org/

- - +

-
-

+				

 package main
 
 import "fmt"
@@ -266,7 +158,7 @@ 

Standard Packages

External Packages

-						
+						
 package main
 
 import (
@@ -304,7 +196,7 @@ 

Packaging Gimmies and Gotchas

Private vs Public

-

+					

 package mypkg
 
 func thisIsPrivate() {}
@@ -318,23 +210,31 @@ 

Private vs Public

Types

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
 					
-
-

Structs

-
- -
-

+
+

Structs

+

 type TinyStruct struct {
 	IsTiny bool
 }
 
+t1 := TinyStruct{}
+t2 := TinyStruct{true}
+t3 := TinyStruct{IsTiny: false}
+t3.IsTiny
+
+http://golang.org/ref/spec#Struct_types +
+
+

Structs

+

 type MyStruct struct {
 	TinyStruct
 	A string
@@ -344,18 +244,45 @@ 

Structs

} } +var ms MyStruct +ms = MyStruct{} +ms.A = "one" +ms.IsTiny = true + +if ms.TinyStruct.IsTiny {...} +
+
+
+

Structs

+

 type OtherStruct struct {
 	MStruct MyStruct
 	OtherField string
 }
+
+os := OtherStruct{MStruct : MyStruct{IsTiny: false}, OtherField : "thing"}
 
-
-
-

Interfaces

+
+ +
+

Functions

+
+
+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 +
+ +
+

Interfaces and Methods

-

+

 type Thinger interface {
 	DoThing() error
 }
@@ -367,24 +294,19 @@ 

Interfaces

func (os *OtherStruct)DoThing() error { return nil } + +func ProcessThinger(t Thinger) error { + return t.DoThing() +}
+http://golang.org/ref/spec#Interface_types
+http://golang.org/ref/spec#Method_declarations

Empty Interface is an Any Type

-
-

Functions

-
-
-func FunctionName(argument string, arg1, arg2 int63) {...}
-func FunctionName(values ...int) error {...}
-func FunctionName()(namedReturn int) {...}
-var f := func() {...}
-
-
-
@@ -411,7 +333,7 @@

RandReader Type

Struct with no public elements
-						
+						
 type RandReader struct {}
 					
 					
@@ -422,7 +344,7 @@

RandReader Type

Holds internal state
-		
+		
 import "math/rand"
 
 type RandReader struct {
@@ -438,7 +360,7 @@ 

NewRandReader() Function

Creates a RandReader and initializes it
-
+
 func NewRandReader() *RandReader {
 	source := rand.NewSource(time.Now().UnixNano())
 	return &RandReader{rand.New(source)}
@@ -451,7 +373,7 @@ 

Attach the Read Function

Supplying a Read([]byte) int, error function makes it match the io.Reader interface
-

+

 import "encoding/binary"
 
 // Contains a bug that does not fill the provided array
@@ -470,17 +392,6 @@ 

Attach the Read Function

Goroutines

-
-

Sidenote: Concurrency vs Parallelism

-
    -
  • - Concurrency: Composition of independently executing processes -
  • -
  • - Parallelism: Simultaneous execution of optionally related computations. -
  • -
-

Sidenote: Concurrency vs Parallelism

@@ -490,11 +401,11 @@

Sidenote: Concurrency vs Parallelism

-- Andrew Gerrand -
-
-

Sidenote: Concurrency vs Parallelism

More Information: Rob Pike on Concurrency vs Parallelism
http://vimeo.com/49718712

+
@@ -511,7 +422,7 @@

Call a standard function

Call a function variable

-

+

 f := func() {time.Sleep(100 * time.Hours())}
 go f()
 
@@ -520,7 +431,7 @@

Call a function variable

Call an anonymous function

-	
+	
 go func() {time.Sleep(100 * time.Hours())}()
 
 
@@ -531,7 +442,7 @@

Goroutine Gotchas

  • Goroutines do not block the main thread from exiting.
  • You must manage concurrent access. See sync.Mutex
  • -
  • There is no one to return a value to. (Use channels)
  • +
  • There is no one to return a value to. (Use channels.)
@@ -547,23 +458,25 @@

Channels

http://blog.golang.org/pipelines
-
-

Make a Channel

-

+
+
+

Make a Channel

+

 var ch chan int
 ch = make(chan int)
 
-
-
+
+ +

Size a Channel

-

+

 ch := make(chan int, 10)
 
-

Send To a Channel

-

+

Send To a Channel

+

 ch := make(chan int, 1)
 ch <- 1
 ch <- 2
@@ -571,15 +484,15 @@ 

Send To a Channel

-

Recieve From a Channel

-

+

Recieve From a Channel

+

 c := <-ch
 
-

Recieve From a Channel with range

-

+

Recieve From a Channel with range

+

 ch := make(chan int, 10)
 ch <- 1
 ch <- 2
@@ -592,8 +505,8 @@ 

Recieve From a Channel with range

-

Recieve From a Channel with select

-

+

Recieve From a Channel with select

+

 ch := make(chan int, 10)
 done := make(chan bool)
 ch <- 1
@@ -613,7 +526,7 @@ 

Recieve From a Channel with select

Return a Channel

-

+

 func generate(nums ...int) <-chan int {
 	out := make(chan int)
 	go func() {
@@ -628,7 +541,7 @@ 

Return a Channel

Pass a Channel

-

+

 func sq(in <-chan int) <-chan int {
     out := make(chan int)
     go func() {
@@ -643,7 +556,7 @@ 

Pass a Channel

A Pipeline of Channels

-

+

 func square(in <-chan int) <-chan int {}
 func generate(nums ...int) <-chan int {}
 
@@ -657,71 +570,120 @@ 

A Pipeline of Channels

-
-
-

Extra! HTTP Service

-
-
-

The Key is an Interface

-

-type Handler interface {
-	ServeHTTP(ResponseWriter, *Request)
-}
-
-
-

Simplest Server

-

-package main
 
-import (
-    "fmt"
-    "net/http"
-)
+			
+
+

Common Toolchain, Common Practices

+
+
    +
  • go clean
  • +
  • go get
  • +
  • go fmt
  • +
  • go test
  • +
  • go build
  • +
+
+
-func handler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) -} +
+

go clean

+
    +
  • Removes object files from package source directories.
  • +
+

+go clean [packages]
+go clean github.com/goraft/raft
+					
+
-func main() { - http.ListenAndServe(":8080", handler) -} -
-
+
+

go get

+
    +
  • Downloads and installs packages and their dependencies.
  • +
+

+go get [-u] [packages]
+go get code.google.com/p/gcfg
+go get -u github.com/goraft/raft
+	
+
-
-

Introducing ServeMux

-

-package main
 
-import (
-		"fmt"
-		"net/http"
-)
+
+

go fmt

+
    +
  • Reformats the packages listed to the golang standard.
  • +
  • Many editors run go fmt on save.
  • +
+

+go fmt [-n] [-x] [packages]
+	
+
-func handler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) -} +
+

go build

+
    +
  • Compiles the packages named by the import paths, along with their dependencies.
  • +
  • Does not install the resulting binaries.
  • +
+

+go build [-o output] [build flags] [packages]
+go build ./...
+	
+
-func main() { - mux := http.NewServeMux() - mux.HandleFunc("/hi",handler) - mux.Handle("",http.NotFoundHandler()) - http.ListenAndServe(":8080", mux) -} +
+

go test

+
    +
  • Recompiles each package along with any files with names matching *_test.go.
  • +
    
    +go test [-c] [-i] [build/test flags] [packages] [flags for test binary]
    +go test ./...
    +go test github.com/goraft/raft
     
    -
+
  • Prints a summary of test results. +
    
    +ok   archive/tar   0.011s
    +FAIL archive/zip   0.022s
    +ok   compress/gzip 0.033s
    +...
    +
  • +
  • Can contain test functions, benchmark functions, and example functions.
  • + +

    http://golang.org/pkg/testing/ +

    + + +
    +

    go install

    +
      +
    • Compiles and installs packages and their dependencies.
    • +
    +
    
    +go install [build flags] [packages]
    +go install github.com/crosbymichael/skydock
    +	
    +
    +
    +
    +

    Top Three Hints

    +
      +
    1. Don't think in traditional objects. Add behavior (interfaces) instead.
    2. +
    3. Try the standard library first.
      Especially "net/http".
    4. +
    5. Learn to love channels.
    6. +

    Resources

    @@ -761,8 +723,12 @@

    We are hiring!

    history: true, center: true, - width: 960, - height: 700, + width: 1800, + height: 1000, + + // Bounds for smallest/largest possible scale to apply to content + minScale: 0.2, + maxScale: 1.0, theme: Reveal.getQueryHash().theme || 'solarized', // available themes are in /css/theme transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/fade/none