This document outlines an agenda for a session on the Go programming language. It covers Go's history and development, grammar and syntax, concurrency features using CSP, the standard library and toolchain, interfacing Go with C, popular production and open source projects using Go, and reference materials. The session aims to provide an overview of Go's key features and how it is used in practice.
3. Section 1: History
• November 10, 2009 : Became a Public Open Source Project
• March 28, 2012 : Go 1.0.0 Release
• August 18,2015 : Latest Version Go 1.5
Release History
Sponsor
Mascot : Gopher Designed By Renée French Who also design Plan9’s mascot Glenda
“Robert Griesemer, Rob Pike and Ken Thompson
started sketching the goals for a new language on the
white board on September 21, 2007. ”
--Golang FAQ
Rust1.0 Released on May 15,2015 And Latest version is Rust1.5 (How Frequently)
TEAM PLAN 9
4. Section 2: Grammar
Structure
• Every Go program is made up of packages. And executable should have “main” package
• “import” is just like “include” in c/c++ and “import” in python. Import other packages
• Package “fmt” has PrintXxx just like “printf” in c/c++ and “print” in python
• Function begin with “func” followed by it’s name.
• “{” should be the same line with “main()” . Think about linux style and Microsoft
• “main” takes no argument like python
5. Section 2: Grammar
Variables & Constants
Type
Namedeclare
variable
Only used in scope
C11 “auto”
declare Name Start with 0
Auto-Increment
Without type
declare
multiple assignment
6. Section 2: Grammar
Basic Types
Container Types
Pointers
Go has pointers. A pointer holds
the memory address of a variable.
• Arrays
• Slices
• Maps
Test if has
Declares a dictionary
Define and assign
Len is element’s count
Cap is max size
7. Section 2: Grammar
Control Flow
• If…else…
Like , statement
• switch
No while in Golang
• for
default break
no condition means TRUE
If..elseif…elseif…else
8. Section 2: Grammar
Function
• ordinary function
• method of struct
• closure
return typeargumentsname
return multiply
values
named return values
a method is just a function
with a receiver argument
a regular function with no change
in functionality
Methods with pointer receivers can
modify the value to which the receiver
points
Functions are values
too. They can be
passed around just
like other values
Lambda
9. Section 2: Grammar
Interface
An interface type is defined as a set of method signatures.
When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck
--Heim, Michael
Golang’s interface is a Duck Type interface
define a interface
implementation of interface
“i” is a interface
variable
Calling a method on an interface value executes the method of the same name on its underlying type
Thought about OOP’s Polymorphisn
10. Section 2: Grammar
Empty Interface & Type Switches
• empty interface
a empty interface is just like
a (void *)
• type switches
1. i.(T) T is a type get the exact type
a false when failed
2. i.(type) return the type of i.
what the real type
a interface holds?
11. Section 2: Grammar
defer
A defer statement defers the execution of a function until the surrounding function returns
defer is a modifier declares a function’s
invoking
should be a
invoking
Outpu
t
invoke releaseB first
then invoke releaseA
Outpu
t
calculate at
defer
12. Section 2: Grammar
panic … recover and error
• panic
panic is just like assert()
• recover
recover will stop panic’s crash
• try..catch..
if g() panic . defer will be invoked .
and recover will get the panic
Go programs express error state with error values. The error type is a built-in interface
Which has “Error() string” return strerror()
• error
14. Section 3: Concurrence
CSP
communicating sequential processes (CSP). It is a member of the family
of mathematical theories of concurrency known as process algebras,
or process calculi, based on message passing via channels.
It is More About a Methodology. Not a Implementation or a Invention.
• Goroutine : a carrier of a task
• Channel : channels to pass message
• Select : tools for channels
• sync.Xxx : assist to solve synchronized problems
Go Concurrency Patterns: Pipelines and cancellation
In each stage, the goroutines
• receive values from upstream via inbound channels
• perform some function on that data, usually producing new values
• send values downstream via outbound channels
15. Section 3: Concurrence
Goruntine
“go”is just like “pthread_create”
“sync.WatiGroup” is like “pthread_join” or “pthread_cond_wait”
But goroutine is not a thread or process of system level
It is like a routine or a light weight thread
16. Section 3: Concurrence
Channel
create a read-write
channel
close channel’s write end
return a channel
receives values from the channel
repeatedly until it is closed
“chan -” : a sent-to only channel
“- chan” : a read only channel
close will cause -
get false with “ok”
17. Section 3: Concurrence
Select
The select statement lets a goroutine wait on multiple
communication operations.
A select blocks until one of its cases can run, then it
executes that case. It chooses one at random if
multiple are ready.
19. Section 3: Concurrence
Benchmark
SNG's IO/CPU Benchmark
The Computer Language Benchmarks Game
Summary
• Golang is similar to C in IO and Simple CPU based Scenes situation
• Compute-intensive situation shouldn’t be Golang’s advantage
21. Section 3: Concurrence
GC
garbage collector is a concurrent, tri-color, mark-
sweep collector, an idea first proposed by Dijkstra in 1978
Go is building a garbage collector (GC) not only for 2015 but for 2025 and beyond: A GC that
supports today’s software development and scales along with new software and hardware
throughout the next decade.
--Golang Blog
Result:
• Not a STW(Stop the World) GC ( = Golang1.4)
• GC in 10ms
• 85% less than before (=Golang1.4)
Principle:
• Believe golang’s team. Golang1.5 1.6 and more
• Write golang with a Cer mind. Maybe you don’t need string pkg
• Write GC friendly codes
36. Section 6: Production OpenSource Projects
OpenSource Projects
Zookeeper(ZAB/Paxos)
K-V Story And Service Discovery (Raft)
Distributed Messaging Platform (Message Queue)
RabbitMQ/Kafak/ZeroMQ/Nanomsg
Web Framework
Djanngo/Tornado/Webpy
Jfinal SSH
Laravel/Flight/Yii
Microservices Framework
37. Section 7: Reference
a. A Tour of Go
b. Effective Go
c. Package Documentation
d. Language Specification
e. The Go Memory Model
f. The Go Blog
g. Go by Example
h. 《 The Go Programming Language 》
i. 《 Go In Action 》
j. 《 Mastering Concurrency in Go 》
Example Code is [HERE]