2. About this presentation
● The point is to give you a "taste" of Go, not
to teach you how to programming in it
● I assume you know what an array and a
pointer is
● I'm not a Go expert
3. Quick history
● Invented by Robert Griesemer, Rob Pike
and Ken Thompson in 2007 (public in '09)
● Born out of a need for ease of programming
combined with type safety and portability
● Other goals:
○ Easy to learn
○ Type safety and memory safety
○ Easy concurrency via channels and "goroutines"
○ Low latency garbage collection
○ Fast compilation
5. Some companies using Go
● Canonical
● BBC
● Heroku
● CloudFoundry
● CloudFlare
● Bit.ly
● Google
6. IDE options
● Sublime Text 2
● IntelliJ
● LiteIDE
● Intype (Windows only)
● Netbeans
● Eclipse
● TextMate
● Komodo
● Zeus (Windows only)
7. Syntax overview
● It looks similar to C
● End of line semicolons are optional
● Variable declarations are optional
○ variable name is followed by it's type
■ var s string
○ variables can also be declared by initialization
■ i := 0
● Type conversions must be explicit
● Visibility is controlled using capitalization eg.
○ func Foo() will be publicly visible outside the
defining package
○ func bar() will be private
8. Interesting caveats
● Unused variables are a compilation error
● Unused import directives also
● No classes - Go is a procedural language
● No overloading
● No inheritance
14. Array
● Numbered sequence of elements of a single
type
● Fixed size
● Always one dimensional but can be
combined
● eg.
○ [32]byte
○ [2][8]int
○ [5]myStruct
○ [3]SomePublicInterface
○ [2]struct { x, y int32 }
○ b := [...]string{"Penn", "Teller"}
15. Slice
● Represents a segment of an array
● Like array - slice is one dimensional
● Slice does not have a fixed length
● Once initialized is always associated with
and underlying array
● Creating a slice:
○ make([]int, 50, 100)
○ new([100]int)[0:50]
○ letters := []string{"a", "b", "c", "d"}
16. Map
● It's a hash table implementeation in Go
● Provides fast lookup by given key
● Key can be any type that is comparable
● Value can be any type including another
map
● Order is not guaranteed
● Maps are not safe for concurrent use!
m := make(map[string]int)
m["val1"] = 1
m["val2"] = 2
for key, value := range m {
fmt.Println("Key:", key, "Value:", value)
}
17. Struct
● Structs are declared using struct keyword
● Only fields can be defined inside a struct
type Vertex struct {
X int
Y int
}
p := Vertex{1, 2}
18. Function
● Can return multiple values
● Unlike in C you can return address of a local
variable
● Functions can be "attached" to a struct or
interface
func NewFile(fd int, name string) *File {
if fd < 0 {
return nil
}
f := File{fd, name, nil, 0}
return &f
}
func (v Vertex)Print() string{
}
19. Sample code
package main
import "fmt"
// fib returns a function that returns successive Fibonacci numbers.
func fib() func() int {
a, b := 0, 1
return func() int {
a, b = b, a+b
return a
}
}
func main() {
f := fib()
// Function calls are evaluated left-to-right.
fmt.Println(f(), f(), f(), f(), f())
}
20. Interface
● Used to specify a behavior of an object
● A type can implement multiple interfaces
● Interfaces can be used in place of concrete
types
● Empty interface is met by any type
type Stringer interface {
ToString() string
}
21. Embedding
● a bit like inheritance
● allows to combine structs or interfaces
type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
// ReadWriter is the interface that combines the Reader and Writer interfaces.
type ReadWriter interface {
Reader
Writer
}
22. Channels and goroutines
"Do not communicate by sharing memory;
instead, share memory by communicating"
● goroutine - is a lightweight concurrent
function (like thread)
● remove complexities of thread management
● channels are used for communicating
between goroutines
● they can be buffered or unbuffered
23. Example of using goroutines and
channels
func sum(a []int, c chan int) {
sum := 0
for _, v := range a {
sum += v
}
c <- sum // send sum to c
}
func main() {
a := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(a[:len(a)/2], c)
go sum(a[len(a)/2:], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x+y)
}
24. What is go good for?
"large programs written by many developers,
growing over time to support networked
services in the cloud: in short, server software"
-- Rob Pike