An Introduction to Go (CERN)
An Introduction to Go (CERN)

        
          

        
        
          

        
        
          
              
                just for func
              
              
          

        
        
          

        
        
          
              
                Day 1
              
              
          

        
        
          
              
                Recording: https://cds.cern.ch/record/2658368
              
              
          

        
        
          

        
        
          
              
                What is Go? An open source (BSD licensed) project: •
              
              Language speciﬁcation, • Small runtime (garbage collector, scheduler, etc), • Two compilers (gc and gccgo), • A standard library, • Tools (build, fetch, test, document, proﬁle, format), • Documentation. Language specs and std library are backwards compatible in Go 1.x.
          

        
        
          

        
        
          
              
                Go is about composition. Composition of: • Types: ◦ The
              
              type system allows bottom-up design. • Processes: ◦ The concurrency principles of Go make process composition straight-forward. • Large scale systems: ◦ The packaging and access control system and Go tooling all help on this. What is Go about?
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          
              
                Some packages are part of the standard library: - “fmt”:
              
              formatting and printing - “encoding/json”: JSON encoding and decoding golang.org/pkg for the whole list Convention: package names match the last element of the import path. import “fmt” → fmt.Println import “math/rand” → rand.Intn More packages
          

        
        
          

        
        
          
              
                A Go workspace resides under a single directory: GOPATH. $
              
              go env GOPATH • defaults to $HOME/go • will maybe disappear soon (Go modules) Three subdirectories: • src: Go source code, your project but also all its dependencies. • bin: Binaries resulting from compilation. • pkg: A cache for compiled packages Understanding GOPATH
          

        
        
          

        
        
          

        
        
          
              
                Type System
              
              
          

        
        
          

        
        
          

        
        
          

        
        
          
              
                abstract types concrete types
              
              
          

        
        
          
              
                abstract types concrete types
              
              
          

        
        
          

        
        
          
              
                The predeﬁned types Numerical: int, int8, int16, int32 (rune), int64
              
              uint, uint8 (byte), uint16, uint32, uint64 complex64, complex128 uintptr Others bool, string, error
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          
              
                You can obtain a section of a slice with the
              
              [:] operator. s := []int{0, 1, 2, 3, 4, 5} // [0, 1, 2, 3, 4, 5] t := s[1:3] // [1, 2] t := u[:3] // [0, 1, 2] t := s[1:] // [1, 2, 3, 4, 5] t[0] = 42 fmt.Println(s) // [0, 42, 2, 3, 4, 5] Sub-slicing
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          
              
                int *os.File *strings.Reader *gzip.Writer []bool
              
              
          

        
        
          
              
                Deﬁning Methods
              
              
          

        
        
          
              
                Methods can be also declared on non-struct types. type Number
              
              int func (n Number) Positive() bool { return n >= 0 } But also: type mathFunc func(float64) float64 func (f mathFunc) Map(xs []float64) []float64 { … } Methods can be deﬁned only on named types deﬁned in this package. Methods can be declared on any named type
          

        
        
          
              
                Go does not support inheritance
              
              
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          
              
                Pros: • The bug is gone! • Runner is completely
              
              independent of RunCounter. • The creation of the Runner can be delayed until (and if) needed. Cons: • We need to explicitly deﬁne the Runner methods on RunCounter: public String getName() { return runner.getName(); } • This can cause lots of repetition, and eventually bugs. Solution: use composition
          

        
        
          

        
        
          
              
                type RunCounter struct { runner Runner; count int} func New(name
              
              string) *RunCounter { return &RunCounter{Runner{name}, 0} } func (r *RunCounter) Run(t Task) { r.count++; r.runner.Run(t) } func (r *RunCounter) RunAll(ts []Task) { r.count += len(ts); r.runner.RunAll(ts) } func (r *RunCounter) Count() int { return r.count } func (r *RunCounter) Name() string { return r.runner.Name() } The Go way: type RunCounter
          

        
        
          

        
        
          
              
                type RunCounter struct { Runner count int } func New(name
              
              string) *RunCounter2 { return &RunCounter{Runner{name}, 0} } func (r *RunCounter) Run(t Task) { r.count++; r.Runner.Run(t) } func (r *RunCounter) RunAll(ts []Task) { r.count += len(ts) r.Runner.RunAll(ts) } func (r *RunCounter) Count() int { return r.count } The Go way: type RunCounter
          

        
        
          

        
        
          
              
                This is the only predeclared type that is not a
              
              concrete. type error interface { Error() string } Error handling is done with error values, not exceptions. if err := doSomething(); err != nil { return fmt.Errorf(“couldn’t do the thing: %v”, err) } The error type
          

        
        
          
              
                abstract types concrete types
              
              
          

        
        
          

        
        
          

        
        
          
              
                int *os.File *strings.Reader *gzip.Writer []bool io.Reader io.Writer
              
              
          

        
        
          

        
        
          
              
                type ReadWriter interface { Reader Writer } union of interfaces
              
              
          

        
        
          
              
                int *os.File *strings.Reader *gzip.Writer []bool io.Reader io.Writer io.ReadWriter
              
              
          

        
        
          
              
                int *os.File *strings.Reader *gzip.Writer []bool io.Reader io.Writer io.ReadWriter ?
              
              
          

        
        
          
              
                interface{}
              
              
          

        
        
          
              
                “interface{} says nothing” - Rob Pike in his Go Proverbs
              
              
          

        
        
          
              
                why do we use interfaces?
              
              
          

        
        
          

        
        
          
              
                so … what’s new?
              
              
          

        
        
          
              
                implicit interface satisfaction
              
              
          

        
        
          
              
                no “implements”
              
              
          

        
        
          
              
                funcdraw
              
              
          

        
        
          

        
        
          

        
        
          
              
                funcdraw package draw package parse
              
              
          

        
        
          
              
                funcdraw with explicit satisfaction package draw package common package parse
              
              
          

        
        
          
              
                funcdraw with implicit satisfaction package draw package parse
              
              
          

        
        
          

        
        
          

        
        
          
              
                interfaces can break dependencies
              
              
          

        
        
          
              
                deﬁne interfaces where you use them
              
              
          

        
        
          
              
                the super power of Go interfaces
              
              
          

        
        
          
              
                type assertions
              
              
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          
              
                Many packages check whether a type satisﬁes an interface: -
              
              fmt.Stringer : implement String() string - json.Marshaler : implement MarshalJSON() ([]byte, error) - json.Unmarshaler : implement UnmarshalJSON([]byte) error - ... and adapt their behavior accordingly. Tip: Always look for exported interfaces in the standard library. type assertions as extension mechanism
          

        
        
          
              
                use type assertions to extend behaviors
              
              
          

        
        
          
              
                Day 2
              
              
          

        
        
          
              
                Recording: https://cds.cern.ch/record/2659409
              
              
          

        
        
          
              
                Concurrency FTW!
              
              
          

        
        
          
              
                Agenda - Live Coding - ... - Q&A
              
              
          

        
        
          
              
                Live Coding Time!
              
              
          

        
        
          

        
        
          

        
        
          
              
                Day 3
              
              
          

        
        
          
              
                Recording: https://cds.cern.ch/record/2659408
              
              
          

        
        
          

        
        
          

        
        
          
              
                Debugging Live Demo! code
              
              
          

        
        
          

        
        
          
              
                Testing Live Demo!
              
              
          

        
        
          

        
        
          
              
                Benchmarking Live Demo!
              
              
          

        
        
          

        
        
          
              
                Benchmarking Live Demo!
              
              
          

        
        
          

        
        
          
              
                Benchmarking Live Demo!
              
              
          

        
        
          

        
        
          
              
                Questions and Answers
              
              
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          

        
        
          
              
                Now it’s your time!
              
              
          

        
        
          
              
                Thanks! Thanks, @francesc [email protected]
              
              
          

        
    