SlideShare a Scribd company logo
Introduction to Google's Go
Stratio / Datio (May 2016)
Mario Castro
For those who don’t know me…
For those who don’t know me…
Mario Castro
DevOps @ Stratio
Started coding with 13 yo
Java (J++) since 2000
PHP, Python, Java Android, Objective-C, Node.js
Pixar's Certi ed in Renderman's render engine
Other scripting languages (MEL, HScript)
Let's Go
When and who...
History
Design began in late 2007.
Robert Griesemer, Rob Pike, and Ken Thompson.
Ian Lance Taylor and Russ Cox.
Open source since 2009 with a very active community.
Language stable as of Go 1, early 2012.
Who's Go
The men behind it...
Ken Thompson (Ex Bell labs, Unix designer, B programming language)
Rob Pike (Ex Bell labs, Unix Team, Inferno, Plan 9, UTF-8)
Robert Griesemer (V8 Chrome engine, Sawzall)
Why a new language
Why a new language
slow builds
uncontrolled dependencies
poor program understanding (code hard to read, poorly documented...)
Computers are enormously quicker but software development is not faster.
Some fundamental concepts such as garbage collection and parallel computation
are not well supported by popular systems languages.
What's Go (as a programming language)
Statically typed (duck typing)
Compiled language (no more virtual machines... thanks...)
Structure oriented (no inheritance)
Concise and simple syntax, easy to get started with
Good facilities for writing concurrent programs that share state by communicating
Uni ed formatting style for the language
Compilation is fast even with large projects
No need to know a new paradigm or awkward syntax
Comparing Go and Java
Go and Java have much in common
Go and Java have much in common
C family (imperative, braces)
Statically typed
Garbage collected
Methods
Interfaces
Type assertions (instanceof)
Re ection
Classes vs Structs. Interfaces vs... Interfaces?
Go and Java have much in common
This is not a competition
Go is not trying to replace Java
Go has focus on productivity and consolidating Google's experience in large
distributed systems
Google uses Java, Javascript, C++, Python, Go, Sawzal and probably a few other
languages are supported (Je Nelson, inventor of Chrome OS)
Go di ers from Java in several ways
Programs compile to machine code. There's no VM.
Statically linked binaries
Built-in strings (UTF-8)
Built-in generic maps and arrays/slices
Built-in concurrency
Go intentionally leaves out many features
No classes
No constructors
No inheritance
No user-de ned generics
No final, exceptions, annotations...
Go intentionally leaves out many features
Seriously...
No, there are not generics nor algebraic types, monads...
Why does Go leave out those features?
Clarity is critical.
classFoo[F[+_]:Monad,A,B](valexecute:Foo.Request[A]=>F[B],valjoins:Foo.Request[A]=>B=
defbar:Foo[({typel[+a]=WriterT[F,Log[A,B],a]})#l,A,B]={
typeTraceW[FF[+_],+AA]=WriterT[FF,Log[A,B],AA]
defexecute(request:Request[A]):WriterT[F,Log[A,B],B]=
self.execute(request).liftM[TraceW]:++>>(repr=>List(request->request.response(repr,self
When reading code, it should be clear what the program will do.
When writing code, it should be clear how to make the program do what you
want.
Sometimes this means writing out a loop instead of invoking an obscure function.
So, Go is not...
...a functional language
...a new fancy way of coding
...a completely open-source project (no pull-requests)
...a super language that will replace us
...talking about replacement... is not the replacement of Java... or C++... or Cobol...
Why Go
Make programming fast
Safety: type-safe and memory-safe
E cient gargage collector
Reduce typing. No more:
foo.Foo*myFoo=newfoo.Foo(foo.FOO_INIT)
Focus on productivity and concurrency of distributed systems with CSP
concurrency model
CSP Concurrency
CSP Concurrency
Actor model: Entities passing messages to each other that are queued (Erlang,
Scala, Java...)
CSP (Communicating sequential processes) model. Proccesses passing messages
to channels that blocks until the messages are taken but with possibility of using
queues
CSP represents the best known way to write software and organize services together
to form a system. Nature itself provides the best examples; even the human body is a
system of interconnected services — respiratory, cardiovascular, nervous, immune,
etc.
The Tao of Hashicorp https://www.hashicorp.com/blog/tao-of-hashicorp.html(https://www.hashicorp.com/blog/tao-of-hashicorp.html)
This was my face when I read the Tao of Hashicorp...
CSP Concurrency
"Gophers" = processes
Arrows = channels
One "Gopher" could listen to zero or more channels
One "Gopher" can send messages to zero or more channels
CSP Concurrency
Process are anonymous (you can just reach them using a channel)
Channels can be bu ered or unbe ered
Channels can be bi-directional or uni-directional
More than one proccess could be listening the same channel
One process could listen more than one channel
More about this in the workshop
An opinionated language
An opinionated language
Can be weird at the beginning
No brackets in new line (formatter will put them up again)
An Uppercase name means that some method or variable is public, lowercase for
private
Comments of public methods must start with the name of the method
Your project must reside within $GOPATH
Parameters in the de nition of a function must be one character
If you return more than one value, last must be an error
No more than one le with "package main" even if you only have one "main"
function (just works with `go build` but not with `go run [ le]`)
Imports points to some kind of URL that must match a path within your $GOPATH
An opinionated language
Hello World
Hello World
packagemain
funcmain(){
println("HelloWorld!")
} Run
Hello world
Hello web-server
packagemain
import(
"fmt"
"log"
"net/http"
)
funcmain(){
http.HandleFunc("/hello",handleHello)
fmt.Println("servingonhttp://localhost:7777/hello")
log.Fatal(http.ListenAndServe("localhost:7777",nil))
}
funchandleHello(whttp.ResponseWriter,req*http.Request){
fmt.Fprintln(w,"Hello,Stratio")
} Run
More examples: Native Unix piping
funcmain(){
c1:=exec.Command("ls","-lh","/")
c2:=exec.Command("awk","{print$5,"011"$9}")
r1,w1:=io.Pipe()
c1.Stdout=w1
c2.Stdin=r1
c2.Stdout=os.Stdout
c1.Start()
c2.Start()
c1.Wait()
w1.Close()
} Run
Inferred types
No need to write variables types
packagemain
import(
"fmt"
"reflect"
)
funcmain(){
me:="JebediahKerman"
fmt.Println(reflect.TypeOf(me))
} Run
Some common coding features and patterns
More than one object on return (error always at the end)
funcmyFunc()(struct1,struct2,error)
Delegate error pattern
returnnil,err //Errorocurred,delegatetocaller
returno,nil //Noerror
Pointers and references
o:=myObject{} //"o"isanobject
doSomething(&o) //Passingapointer."o"isstillanobject
o:=&myObject{} //Pointertoobject."o"isapointer
funcuseObject(o*myObject) //Areceivedpointer"o"isapointer
Some commong coding features and patterns
Handle every error
iferr!=nil{
log.Fatal(err)
}
Ignore a returned variable (very bad practice, don't trust code that do this)
myobj,_:=myFuncThatReturnsAnObjectAndAnError()
Return anything (quite common in Consul code)
funcmyFunc()interface{}
Do some type assertion
obj:=myFunc()
myString,ok:=obj.(string)
ifok{
println(myString)
}
Testing
Comes with its own test suite
$gotest.
$ok github.com/thehivecorporation/raccoon/parser 0.003s
Getting output...
$gotest-v.
$INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/i-do-not-exist
$INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/wrongJson81990
$INFO[0000]------------------------------>Readingzombiebookfile zombiebook=../examples/example
$INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/no-content6184
$INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/incorrect-keys
$INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/incorrect-keys
$INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/incorrect-keys
$INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/incorrect-keys
$---PASS:TestReadZbookFile(0.00s)
$ zbook_test.go:18:Errorreadingzombiebookfile:open/tmp/i-do-not-exist:nosuchfileordir
$ zbook_test.go:32:ErrorparsingJSON:invalidcharacter'w'lookingforbeginningofobjectke
$PASS
$ok github.com/thehivecorporation/raccoon/parser 0.003s
Testing
Testing
Race condition evaluation integrated
$gotest-race.
$ok github.com/thehivecorporation/raccoon/parser 1.013s
Comes with its own coverage support
$gotest-race-cover.
$ok github.com/thehivecorporation/raccoon/parser 1.011s coverage:68.3%ofstatements
Other tools
gofmt
gofmtformats indentation, spaces, newlines and syntax
Makes most code familiar.
golint
golintwarns you about good practices and suggestions
Example:
typeExecutorinterface{
Execute()
}
linter.go:3:6:exportedtypeExecutorshouldhavecommentorbeunexported
Ok so...
//Interfaceforexecution
typeExecutorinterface{
Execute()
}
linter2.go:3:1:commentonexportedtypeExecutorshouldbeoftheform"Executor..."(withoptiona
//Executoristhestrategypatternfora...
typeExecutorinterface{
Execute()
}
Introduction to Google's Go programming language
golint
golint
If the name always begins the comment, the output of godoc can usefully be run
through grep. Imagine you couldn't remember the name "Compile" but were
looking for the parsing function for regular expressions, so you ran the command,
$godocregexp|grepparse
Compileparsesaregularexpressionandreturns,ifsuccessful,aRegexp
parsed.Itsimplifiessafeinitializationofglobalvariablesholding
cannotbeparsed.Itsimplifiessafeinitializationofglobalvariables
$
Found in "E ective Go"(https://golang.org/doc/e ective_go.html#commentary)
GoDoc
All core packages at your ngertips
You can perform queries in command line
Or open a web server to search
godocindex=true-http=:6060
Search in a speci c package
godocfmt|grep-iread
funcFscanln(rio.Reader,a...interface{})(nint,errerror)
Scanscanstextreadfromstandardinput,storingsuccessive
Scanfscanstextreadfromstandardinput,storingsuccessive
//ReadRunereadsthenextrune(Unicodecodepoint)fromtheinput.
//IfinvokedduringScanln,Fscanln,orSscanln,ReadRune()will
//returnEOFafterreturningthefirst'n'orwhenreadingbeyond
ReadRune()(rrune,sizeint,errerror)
//UnreadRunecausesthenextcalltoReadRunetoreturnthesamerune.
UnreadRune()error
//BecauseReadRuneisimplementedbytheinterface,Readshouldneverbe
//ScanStatemaychoosealwaystoreturnanerrorfromRead.
Read(buf[]byte)(nint,errerror)
Go Get
Useful for dependency management
As simple as
$gogetgithub.com/kubernetes/kubernetes
import"github.com/4ad/doozer"
varclientdoozer.Conn
Most IDE's has an auto-import (on save) features
Work ow
Work ow
Installing Go
From repositories
- Ubuntu users: `sudo apt-get install -y golang`
- Centos/RHEL users: `sudo yum install -y golang`
- Fedora: `sudo dnf install -y golang`
Installing Go
Ok... the slightly harder one: Installing Go
Latest build
golang.org(http://golang.org)
Select your distro
unpack it `tar -zxvf go1.6.2.linux-amd64.tar.gz`
Move the gofolder to your favorite destination (assign permissions according to
destination)
Add a $GOROOT environment variable pointing to your go folder (not the bin
folder within your go folder) to /etc/pro le, ${HOME}/.bashrc, etc.
Add a $GOPATH environment variable pointing to an empty folder that will
represent your global workspace for Go
Add $GOPATH/bin and $GOROOT/bin to your $PATH
The workspace
Workspace is global and it's de ned as an environment variable called $GOPATH.
$exportGOPATH=${HOME}/go
So a `go get github.com/docker/docker` will put the source in
$gogetgithub.com/docker/docker
$cd$GOPATH/src/github.com/docker/docker
Easy, uh?
Our rst Go App
packagemain
funcmain(){
println("Helloworld")
} Run
Our second Go App
packagemain
import"fmt"
funcmain(){
fmt.Printf("(P1)Hello")
goWorld()
}
funcWorld(){
fmt.Printf("world(P2)n")
}
Our third Go App
packagemain
import"fmt"
funcmain(){
fori:=0;i<10000;i++{
gofunc(jint){
fmt.Printf("Proccess#%dn",j)
}(i)
}
varinputstring
fmt.Scanln(&input)
}
Our fourth Go App
funcmain(){
workersCh:=make(chanint,20)
fori:=0;i<20;i++{
goworker(i,workersCh)
}
iter:=0
for{
workersCh<-iter
iter++
time.Sleep(100*time.Millisecond)
}
}
funcworker(idint,wchanint){
for{
select{
casenumber:=<-w:
fmt.Printf("Worker%dgotthenumber%dn",id,number)
time.Sleep(3*time.Second)
}
}
}//END
Our fth Go App
vardelaytime.Duration=5000
funcmain(){
dispatcherCh:=make(chanint,100)
workersCh:=make(chanint)
fori:=0;i<20;i++{
goworker(i,workersCh)
}
godispatcher(dispatcherCh,workersCh)
iter:=0
for{
fmt.Printf("Queuehassize%dn",len(dispatcherCh))
iflen(dispatcherCh)==100{
delay=100
}elseiflen(dispatcherCh)==0{
delay=5000
}
dispatcherCh<-iter
iter++
time.Sleep(100*time.Millisecond)
}
}
funcdispatcher(c,wchanint){
for{
v:=<-c
w<-v
}
}
funcworker(idint,wchanint){
for{
select{
casenumber:=<-w:
fmt.Printf("Worker%dgotthenumber%dn",id,number)
time.Sleep(delay*time.Millisecond)
}
}
}//END
Our sixth...
Contributing in Github
Work ow is slightly di erent
Fork the project into your own github account
Don't clone your fork! Use `go get` of the original git project
As mentioned earlier, it will create a folder within your
$GOPATH/src/[project_owner]/[repo]
Add your fork as a di erente remote (`git remote add my_fork
https://github.com/[my_account]/[my_fork]`)
Work as usual
Push your changes to your fork
Open pull request as usual
(At least, this is how I do it)
Part of Tens
No inheritance
Strongly typed
No “git clone”. Use “go get”
Strongly opinionated
No generics
Has pointers and references
No unused variables nor imports
Strings and structs can’t be nil
You must work always in $GOPATH
No need of locks or semaphores with channels
IDE's and other editing tools
vim + vim-go
emacs + go-model.el
Intellij Idea (has debugging support already. Thanks Abel!)
Atom + go-plus (Has debugging support using Delve plugin)
Sublime Text + GoSublime
Visual Studio Code + vscode-go
LiteIde
Companies using Go
Google
Docker
Net ix
Parse
Digital Ocean
Ebay
AirBnB
Dropbox
Uber
VMWare
Famous software written in Go
Docker
Kubernetes (Google)
Hashicorp's stack (Consul, Terraform, Vault, Serf, Otto, Nomad, Packer)
Prometheus (SoundClound)
CoreOS stack (ETCD, Fleet, Rkt, Flannel)
In uxDB
Grafana
Best resources to learn Go (ordered)
A Tour of Go (interactive tutorial)
https://tour.golang.org/list(https://tour.golang.org/list)
Go By Example
https://gobyexample.com/(https://gobyexample.com/)
How to install Go workspace
https://golang.org/doc/code.html(https://golang.org/doc/code.html)
Go bootcamp (free ebook)
http://www.golangbootcamp.com/(http://www.golangbootcamp.com/)
"E ective Go" Wait a week or two to read
https://golang.org/doc/e ective_go.html(https://golang.org/doc/e ective_go.html)
Other references
Docker and Go: Why we decide to write Docker in Go?
http://www.slideshare.net/jpetazzo/docker-and-go-why-did-we-decide-to-write-
docker-in-go(http://www.slideshare.net/jpetazzo/docker-and-go-why-did-we-decide-to-write-docker-in-go)
By the way
This presentation...
...isalsodonewithaGotool
Questions?
Thank you
Mario Castro
Gopher @ StratioBD(mailto:Gopher%20@%20StratioBD)
@110010111101011(http://twitter.com/110010111101011)
github.com/sayden
mcastro@stratio.com(mailto:mcastro@stratio.com)
Introduction to Google's Go programming language

More Related Content

PDF
Getting Started with Go
PPT
2007 09 10 Fzi Training Groovy Grails V Ws
PDF
Go for Object Oriented Programmers or Object Oriented Programming without Obj...
PDF
A Recovering Java Developer Learns to Go
PDF
7 Common mistakes in Go and when to avoid them
PDF
Inside the JVM - Follow the white rabbit!
PDF
Seeking Clojure
Getting Started with Go
2007 09 10 Fzi Training Groovy Grails V Ws
Go for Object Oriented Programmers or Object Oriented Programming without Obj...
A Recovering Java Developer Learns to Go
7 Common mistakes in Go and when to avoid them
Inside the JVM - Follow the white rabbit!
Seeking Clojure

What's hot (20)

PDF
7 Common Mistakes in Go (2015)
PDF
Introduction to go language programming
PDF
Painless Data Storage with MongoDB & Go
PPTX
2016 bioinformatics i_python_part_1_wim_vancriekinge
PDF
Go for Rubyists
PPTX
2016 bioinformatics i_python_part_2_strings_wim_vancriekinge
PDF
Intro to Python for Non-Programmers
PDF
An Introduction to Groovy for Java Developers
PPT
Groovy for Java Developers
PDF
Groovy for java developers
PPTX
Python in 30 minutes!
ODP
Groovy and Grails intro
ODP
Dynamic Python
PPTX
Go Language Hands-on Workshop Material
PDF
Programming with Python - Basic
PDF
Lisp for Python Programmers
PPTX
Python basics
PPTX
Learn python – for beginners
PPTX
Best Python Online Training with Live Project by Expert
PPTX
Python Seminar PPT
7 Common Mistakes in Go (2015)
Introduction to go language programming
Painless Data Storage with MongoDB & Go
2016 bioinformatics i_python_part_1_wim_vancriekinge
Go for Rubyists
2016 bioinformatics i_python_part_2_strings_wim_vancriekinge
Intro to Python for Non-Programmers
An Introduction to Groovy for Java Developers
Groovy for Java Developers
Groovy for java developers
Python in 30 minutes!
Groovy and Grails intro
Dynamic Python
Go Language Hands-on Workshop Material
Programming with Python - Basic
Lisp for Python Programmers
Python basics
Learn python – for beginners
Best Python Online Training with Live Project by Expert
Python Seminar PPT
Ad

Similar to Introduction to Google's Go programming language (20)

PDF
Introduction to Programming in Go
PDF
Inroduction to golang
ODP
Programming Under Linux In Python
PPTX
The GO Language : From Beginners to Gophers
PDF
Beginning development in go
PDF
Introduction to clojure
PPTX
Sinfonier: How I turned my grandmother into a data analyst - Fran J. Gomez - ...
PPT
GWT is Smarter Than You
PDF
Javascript, DOM, browsers and frameworks basics
PPT
A First Look at Google's Go Programming Language
PPT
Google's Go Programming Language - Introduction
ZIP
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
PDF
Sugar Presentation - YULHackers March 2009
PPTX
PPTX
Go from a PHP Perspective
PDF
Python for Linux System Administration
PPT
Groovy Update - JavaPolis 2007
PPTX
Java - A broad introduction
PDF
Hop, a language for programming the web 2.0
PDF
Full-stack go with GopherJS
Introduction to Programming in Go
Inroduction to golang
Programming Under Linux In Python
The GO Language : From Beginners to Gophers
Beginning development in go
Introduction to clojure
Sinfonier: How I turned my grandmother into a data analyst - Fran J. Gomez - ...
GWT is Smarter Than You
Javascript, DOM, browsers and frameworks basics
A First Look at Google's Go Programming Language
Google's Go Programming Language - Introduction
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Sugar Presentation - YULHackers March 2009
Go from a PHP Perspective
Python for Linux System Administration
Groovy Update - JavaPolis 2007
Java - A broad introduction
Hop, a language for programming the web 2.0
Full-stack go with GopherJS
Ad

Recently uploaded (20)

PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Welding lecture in detail for understanding
PDF
composite construction of structures.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Sustainable Sites - Green Building Construction
PPT
Project quality management in manufacturing
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Construction Project Organization Group 2.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Foundation to blockchain - A guide to Blockchain Tech
Welding lecture in detail for understanding
composite construction of structures.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Sustainable Sites - Green Building Construction
Project quality management in manufacturing
R24 SURVEYING LAB MANUAL for civil enggi
Automation-in-Manufacturing-Chapter-Introduction.pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Construction Project Organization Group 2.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Lecture Notes Electrical Wiring System Components
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT

Introduction to Google's Go programming language

  • 1. Introduction to Google's Go Stratio / Datio (May 2016) Mario Castro
  • 2. For those who don’t know me…
  • 3. For those who don’t know me… Mario Castro DevOps @ Stratio Started coding with 13 yo Java (J++) since 2000 PHP, Python, Java Android, Objective-C, Node.js Pixar's Certi ed in Renderman's render engine Other scripting languages (MEL, HScript)
  • 6. History Design began in late 2007. Robert Griesemer, Rob Pike, and Ken Thompson. Ian Lance Taylor and Russ Cox. Open source since 2009 with a very active community. Language stable as of Go 1, early 2012.
  • 7. Who's Go The men behind it... Ken Thompson (Ex Bell labs, Unix designer, B programming language) Rob Pike (Ex Bell labs, Unix Team, Inferno, Plan 9, UTF-8) Robert Griesemer (V8 Chrome engine, Sawzall)
  • 8. Why a new language
  • 9. Why a new language slow builds uncontrolled dependencies poor program understanding (code hard to read, poorly documented...) Computers are enormously quicker but software development is not faster. Some fundamental concepts such as garbage collection and parallel computation are not well supported by popular systems languages.
  • 10. What's Go (as a programming language) Statically typed (duck typing) Compiled language (no more virtual machines... thanks...) Structure oriented (no inheritance) Concise and simple syntax, easy to get started with Good facilities for writing concurrent programs that share state by communicating Uni ed formatting style for the language Compilation is fast even with large projects No need to know a new paradigm or awkward syntax
  • 12. Go and Java have much in common
  • 13. Go and Java have much in common C family (imperative, braces) Statically typed Garbage collected Methods Interfaces Type assertions (instanceof) Re ection
  • 14. Classes vs Structs. Interfaces vs... Interfaces?
  • 15. Go and Java have much in common
  • 16. This is not a competition Go is not trying to replace Java Go has focus on productivity and consolidating Google's experience in large distributed systems Google uses Java, Javascript, C++, Python, Go, Sawzal and probably a few other languages are supported (Je Nelson, inventor of Chrome OS)
  • 17. Go di ers from Java in several ways Programs compile to machine code. There's no VM. Statically linked binaries Built-in strings (UTF-8) Built-in generic maps and arrays/slices Built-in concurrency
  • 18. Go intentionally leaves out many features No classes No constructors No inheritance No user-de ned generics No final, exceptions, annotations...
  • 19. Go intentionally leaves out many features
  • 20. Seriously... No, there are not generics nor algebraic types, monads...
  • 21. Why does Go leave out those features? Clarity is critical. classFoo[F[+_]:Monad,A,B](valexecute:Foo.Request[A]=>F[B],valjoins:Foo.Request[A]=>B= defbar:Foo[({typel[+a]=WriterT[F,Log[A,B],a]})#l,A,B]={ typeTraceW[FF[+_],+AA]=WriterT[FF,Log[A,B],AA] defexecute(request:Request[A]):WriterT[F,Log[A,B],B]= self.execute(request).liftM[TraceW]:++>>(repr=>List(request->request.response(repr,self When reading code, it should be clear what the program will do. When writing code, it should be clear how to make the program do what you want. Sometimes this means writing out a loop instead of invoking an obscure function.
  • 22. So, Go is not... ...a functional language ...a new fancy way of coding ...a completely open-source project (no pull-requests) ...a super language that will replace us ...talking about replacement... is not the replacement of Java... or C++... or Cobol...
  • 23. Why Go Make programming fast Safety: type-safe and memory-safe E cient gargage collector Reduce typing. No more: foo.Foo*myFoo=newfoo.Foo(foo.FOO_INIT) Focus on productivity and concurrency of distributed systems with CSP concurrency model
  • 25. CSP Concurrency Actor model: Entities passing messages to each other that are queued (Erlang, Scala, Java...) CSP (Communicating sequential processes) model. Proccesses passing messages to channels that blocks until the messages are taken but with possibility of using queues CSP represents the best known way to write software and organize services together to form a system. Nature itself provides the best examples; even the human body is a system of interconnected services — respiratory, cardiovascular, nervous, immune, etc. The Tao of Hashicorp https://www.hashicorp.com/blog/tao-of-hashicorp.html(https://www.hashicorp.com/blog/tao-of-hashicorp.html) This was my face when I read the Tao of Hashicorp...
  • 26. CSP Concurrency "Gophers" = processes Arrows = channels One "Gopher" could listen to zero or more channels One "Gopher" can send messages to zero or more channels
  • 27. CSP Concurrency Process are anonymous (you can just reach them using a channel) Channels can be bu ered or unbe ered Channels can be bi-directional or uni-directional More than one proccess could be listening the same channel One process could listen more than one channel More about this in the workshop
  • 29. An opinionated language Can be weird at the beginning No brackets in new line (formatter will put them up again) An Uppercase name means that some method or variable is public, lowercase for private Comments of public methods must start with the name of the method Your project must reside within $GOPATH Parameters in the de nition of a function must be one character If you return more than one value, last must be an error No more than one le with "package main" even if you only have one "main" function (just works with `go build` but not with `go run [ le]`) Imports points to some kind of URL that must match a path within your $GOPATH
  • 35. More examples: Native Unix piping funcmain(){ c1:=exec.Command("ls","-lh","/") c2:=exec.Command("awk","{print$5,"011"$9}") r1,w1:=io.Pipe() c1.Stdout=w1 c2.Stdin=r1 c2.Stdout=os.Stdout c1.Start() c2.Start() c1.Wait() w1.Close() } Run
  • 36. Inferred types No need to write variables types packagemain import( "fmt" "reflect" ) funcmain(){ me:="JebediahKerman" fmt.Println(reflect.TypeOf(me)) } Run
  • 37. Some common coding features and patterns More than one object on return (error always at the end) funcmyFunc()(struct1,struct2,error) Delegate error pattern returnnil,err //Errorocurred,delegatetocaller returno,nil //Noerror Pointers and references o:=myObject{} //"o"isanobject doSomething(&o) //Passingapointer."o"isstillanobject o:=&myObject{} //Pointertoobject."o"isapointer funcuseObject(o*myObject) //Areceivedpointer"o"isapointer
  • 38. Some commong coding features and patterns Handle every error iferr!=nil{ log.Fatal(err) } Ignore a returned variable (very bad practice, don't trust code that do this) myobj,_:=myFuncThatReturnsAnObjectAndAnError() Return anything (quite common in Consul code) funcmyFunc()interface{} Do some type assertion obj:=myFunc() myString,ok:=obj.(string) ifok{ println(myString) }
  • 39. Testing Comes with its own test suite $gotest. $ok github.com/thehivecorporation/raccoon/parser 0.003s Getting output... $gotest-v. $INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/i-do-not-exist $INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/wrongJson81990 $INFO[0000]------------------------------>Readingzombiebookfile zombiebook=../examples/example $INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/no-content6184 $INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/incorrect-keys $INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/incorrect-keys $INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/incorrect-keys $INFO[0000]------------------------------>Readingzombiebookfile zombiebook=/tmp/incorrect-keys $---PASS:TestReadZbookFile(0.00s) $ zbook_test.go:18:Errorreadingzombiebookfile:open/tmp/i-do-not-exist:nosuchfileordir $ zbook_test.go:32:ErrorparsingJSON:invalidcharacter'w'lookingforbeginningofobjectke $PASS $ok github.com/thehivecorporation/raccoon/parser 0.003s
  • 41. Testing Race condition evaluation integrated $gotest-race. $ok github.com/thehivecorporation/raccoon/parser 1.013s Comes with its own coverage support $gotest-race-cover. $ok github.com/thehivecorporation/raccoon/parser 1.011s coverage:68.3%ofstatements
  • 43. gofmt gofmtformats indentation, spaces, newlines and syntax Makes most code familiar.
  • 44. golint golintwarns you about good practices and suggestions Example: typeExecutorinterface{ Execute() } linter.go:3:6:exportedtypeExecutorshouldhavecommentorbeunexported Ok so... //Interfaceforexecution typeExecutorinterface{ Execute() } linter2.go:3:1:commentonexportedtypeExecutorshouldbeoftheform"Executor..."(withoptiona //Executoristhestrategypatternfora... typeExecutorinterface{ Execute() }
  • 47. golint If the name always begins the comment, the output of godoc can usefully be run through grep. Imagine you couldn't remember the name "Compile" but were looking for the parsing function for regular expressions, so you ran the command, $godocregexp|grepparse Compileparsesaregularexpressionandreturns,ifsuccessful,aRegexp parsed.Itsimplifiessafeinitializationofglobalvariablesholding cannotbeparsed.Itsimplifiessafeinitializationofglobalvariables $ Found in "E ective Go"(https://golang.org/doc/e ective_go.html#commentary)
  • 48. GoDoc All core packages at your ngertips You can perform queries in command line Or open a web server to search godocindex=true-http=:6060 Search in a speci c package godocfmt|grep-iread funcFscanln(rio.Reader,a...interface{})(nint,errerror) Scanscanstextreadfromstandardinput,storingsuccessive Scanfscanstextreadfromstandardinput,storingsuccessive //ReadRunereadsthenextrune(Unicodecodepoint)fromtheinput. //IfinvokedduringScanln,Fscanln,orSscanln,ReadRune()will //returnEOFafterreturningthefirst'n'orwhenreadingbeyond ReadRune()(rrune,sizeint,errerror) //UnreadRunecausesthenextcalltoReadRunetoreturnthesamerune. UnreadRune()error //BecauseReadRuneisimplementedbytheinterface,Readshouldneverbe //ScanStatemaychoosealwaystoreturnanerrorfromRead. Read(buf[]byte)(nint,errerror)
  • 49. Go Get Useful for dependency management As simple as $gogetgithub.com/kubernetes/kubernetes import"github.com/4ad/doozer" varclientdoozer.Conn Most IDE's has an auto-import (on save) features
  • 52. Installing Go From repositories - Ubuntu users: `sudo apt-get install -y golang` - Centos/RHEL users: `sudo yum install -y golang` - Fedora: `sudo dnf install -y golang`
  • 54. Ok... the slightly harder one: Installing Go Latest build golang.org(http://golang.org) Select your distro unpack it `tar -zxvf go1.6.2.linux-amd64.tar.gz` Move the gofolder to your favorite destination (assign permissions according to destination) Add a $GOROOT environment variable pointing to your go folder (not the bin folder within your go folder) to /etc/pro le, ${HOME}/.bashrc, etc. Add a $GOPATH environment variable pointing to an empty folder that will represent your global workspace for Go Add $GOPATH/bin and $GOROOT/bin to your $PATH
  • 55. The workspace Workspace is global and it's de ned as an environment variable called $GOPATH. $exportGOPATH=${HOME}/go So a `go get github.com/docker/docker` will put the source in $gogetgithub.com/docker/docker $cd$GOPATH/src/github.com/docker/docker Easy, uh?
  • 56. Our rst Go App packagemain funcmain(){ println("Helloworld") } Run
  • 57. Our second Go App packagemain import"fmt" funcmain(){ fmt.Printf("(P1)Hello") goWorld() } funcWorld(){ fmt.Printf("world(P2)n") }
  • 58. Our third Go App packagemain import"fmt" funcmain(){ fori:=0;i<10000;i++{ gofunc(jint){ fmt.Printf("Proccess#%dn",j) }(i) } varinputstring fmt.Scanln(&input) }
  • 59. Our fourth Go App funcmain(){ workersCh:=make(chanint,20) fori:=0;i<20;i++{ goworker(i,workersCh) } iter:=0 for{ workersCh<-iter iter++ time.Sleep(100*time.Millisecond) } } funcworker(idint,wchanint){ for{ select{ casenumber:=<-w: fmt.Printf("Worker%dgotthenumber%dn",id,number) time.Sleep(3*time.Second) } } }//END
  • 60. Our fth Go App vardelaytime.Duration=5000 funcmain(){ dispatcherCh:=make(chanint,100) workersCh:=make(chanint) fori:=0;i<20;i++{ goworker(i,workersCh) } godispatcher(dispatcherCh,workersCh) iter:=0 for{ fmt.Printf("Queuehassize%dn",len(dispatcherCh)) iflen(dispatcherCh)==100{ delay=100 }elseiflen(dispatcherCh)==0{ delay=5000 } dispatcherCh<-iter iter++ time.Sleep(100*time.Millisecond) } }
  • 63. Contributing in Github Work ow is slightly di erent Fork the project into your own github account Don't clone your fork! Use `go get` of the original git project As mentioned earlier, it will create a folder within your $GOPATH/src/[project_owner]/[repo] Add your fork as a di erente remote (`git remote add my_fork https://github.com/[my_account]/[my_fork]`) Work as usual Push your changes to your fork Open pull request as usual (At least, this is how I do it)
  • 64. Part of Tens No inheritance Strongly typed No “git clone”. Use “go get” Strongly opinionated No generics Has pointers and references No unused variables nor imports Strings and structs can’t be nil You must work always in $GOPATH No need of locks or semaphores with channels
  • 65. IDE's and other editing tools vim + vim-go emacs + go-model.el Intellij Idea (has debugging support already. Thanks Abel!) Atom + go-plus (Has debugging support using Delve plugin) Sublime Text + GoSublime Visual Studio Code + vscode-go LiteIde
  • 66. Companies using Go Google Docker Net ix Parse Digital Ocean Ebay AirBnB Dropbox Uber VMWare
  • 67. Famous software written in Go Docker Kubernetes (Google) Hashicorp's stack (Consul, Terraform, Vault, Serf, Otto, Nomad, Packer) Prometheus (SoundClound) CoreOS stack (ETCD, Fleet, Rkt, Flannel) In uxDB Grafana
  • 68. Best resources to learn Go (ordered) A Tour of Go (interactive tutorial) https://tour.golang.org/list(https://tour.golang.org/list) Go By Example https://gobyexample.com/(https://gobyexample.com/) How to install Go workspace https://golang.org/doc/code.html(https://golang.org/doc/code.html) Go bootcamp (free ebook) http://www.golangbootcamp.com/(http://www.golangbootcamp.com/) "E ective Go" Wait a week or two to read https://golang.org/doc/e ective_go.html(https://golang.org/doc/e ective_go.html)
  • 69. Other references Docker and Go: Why we decide to write Docker in Go? http://www.slideshare.net/jpetazzo/docker-and-go-why-did-we-decide-to-write- docker-in-go(http://www.slideshare.net/jpetazzo/docker-and-go-why-did-we-decide-to-write-docker-in-go)
  • 73. Thank you Mario Castro Gopher @ StratioBD(mailto:Gopher%20@%20StratioBD) @110010111101011(http://twitter.com/110010111101011) github.com/sayden mcastro@stratio.com(mailto:mcastro@stratio.com)