SlideShare a Scribd company logo
The Go features I can't live without,
2nd round
Golang Brno meetup #2
16 June 2016
Rodolfo Carvalho
Red Hat
Previously
goo.gl/ZkHw4X
1. Simplicity
2. Single dispatch
3. Capitalization
4. gofmt
5. godoc
6. No exceptions
7. Table-Driven Tests
8. Interfaces
9
First-class functions
In Go, functions are rst-class citizens.
They can be taken as argument, returned as value, assigned to variables, and so on.
Trivial, but not all languages have it... Bash nightmares!
You know what, you can even implement methods on function types!
//FromGo'snet/http/server.go
typeHandlerFuncfunc(ResponseWriter,*Request)
func(fHandlerFunc)ServeHTTP(wResponseWriter,r*Request){
f(w,r)
}
Example
funcmain(){
cmd:=exec.Command("bash","-c","whiletrue;dodate&&sleep1;done")
cmd.Stdout=os.Stdout
iferr:=cmd.Start();err!=nil{
log.Fatal(err)
}
timeout:=5*time.Second
time.AfterFunc(timeout,func(){
cmd.Process.Kill()
})
iferr:=cmd.Wait();err!=nil{
log.Fatal(err)
}
} Run
10
Fully quali ed imports
Easy to tell where a package comes from.
packagebuilder
import(
"fmt"
"io/ioutil"
"os"
"os/exec"
"time"
"github.com/docker/docker/builder/parser"
kapi"k8s.io/kubernetes/pkg/api"
"github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/client"
)
Compare with Ruby
require'rubygems'
require'openshift-origin-node/model/frontend/http/plugins/frontend_http_base'
require'openshift-origin-node/utils/shell_exec'
require'openshift-origin-node/utils/node_logger'
require'openshift-origin-node/utils/environ'
require'openshift-origin-node/model/application_container'
require'openshift-origin-common'
require'fileutils'
require'openssl'
require'fcntl'
require'json'
require'tmpdir'
require'net/http'
Now we need a way to determine where each of those things come from.
Imports in Go
Plain strings, give exibility to language spec.
"The interpretation of the ImportPath is implementation-dependent but it is typically a substring
of the full le name of the compiled package and may be relative to a repository of installed
packages."
Path relative to GOPATH.
PackageName != ImportPath; by convention, the package name is the base name of its
source directory.
Making imports be valid URLs allows tooling (goget) to automate downloading
dependencies.
Note: fully quali ed imports doesn't solve package versioning.
11
Static typing with type inference
Stutter-fee static typing
Let the compiler type check, not unit tests
Easier refactorings
cmd:=exec.Command("yes")
varcmd=exec.Command("yes")
varcmd*exec.Cmd=exec.Command("yes")
varcmd*exec.Cmd
cmd=exec.Command("yes")
12
Speed
Development
Compilation
Execution
Just a super cial reason to justify Go's success.
What I like
Go is a compiled language that feels like scripting*.
Minimal boilerplate:
No need* for con g les, build scripts, header les, XML les, etc.
The only con guration is GOPATH.
* But not all the time...
E.g., big projects like OpenShift can take 330+s to build with Go 1.6 ☹
Fast feedback cycles
packagemain
import"fmt"
funcmain(){
fmt.Println("HelloBrno!")
} Run
How it improved over time
40
30
20
10
0
Speed (m/s)
1.0 2.0 3.0 4.0 5.0 6.0
Time (s)
13
Metaprogramming
Wikipedia says: ... the writing of computer programs with the ability to treat programs as their
data. It means that a program could be designed to read, generate, analyse or transform other
programs, and even modify itself while running.
Go has no support for generics, at least yet.
No macros.
But has:
re ect package.
Packages in the stdlib for parsing, type checking and manipulating Go code.
Go programs that write Go programs and can serve as example: gofmt, goimports,
stringer, gofix, etc.
gogeneratetool.
14
Static linking
The linker creates statically-linked binaries by default.
Easy to deploy/distribute.
Big binary sizes.
OpenShift is a single binary with 134 MB today. Includes server, client, numerous command
line tools, Web Console, ...
Tale: distributing a Python program.
15
Cross-compilation
Develop/build on your preferred platform, deploy anywhere.
Operating Systems:
Linux
OS X
Windows
*BSD, Plan 9, Solaris, NaCl, Android
Architectures:
amd64
x86
arm, arm64, ppc64, ppc64le, mips64, mips64le
Easy to use
$GOOS=linux GOARCH=armGOARM=7gobuild-ofunc-linux-arm7 func.go
$GOOS=linux GOARCH=armGOARM=6gobuild-ofunc-linux-arm6 func.go
$GOOS=linux GOARCH=386 gobuild-ofunc-linux-386 func.go
$GOOS=windowsGOARCH=386 gobuild-ofunc-windows-386func.go
$filefunc-*
func-linux-386: ELF32-bitLSBexecutable,Intel80386,version1(SYSV),
staticallylinked,notstripped
func-linux-arm6: ELF32-bitLSBexecutable,ARM,EABI5version1(SYSV),
staticallylinked,notstripped
func-linux-arm7: ELF32-bitLSBexecutable,ARM,EABI5version1(SYSV),
staticallylinked,notstripped
func-windows-386:PE32executable(console)Intel80386(strippedtoexternal
PDB),forMSWindows
$dufunc-*
2184 func-linux-386
2188 func-linux-arm6
2184 func-linux-arm7
2368 func-windows-386
16
Go Proverbs
Similar to The Zen of Python, there is a lot of accumulated experience and shared knowledge
expressed by Go Proverbs.
Not actually a feature, but a "philosophical guidance".
Go Proverbs 1/4
Don't communicate by sharing memory, share memory by communicating.
Concurrency is not parallelism.
Channels orchestrate; mutexes serialize.
The bigger the interface, the weaker the abstraction.
Make the zero value useful.
Go Proverbs 2/4
interface{} says nothing.
Gofmt's style is no one's favorite, yet gofmt is everyone's favorite.
A little copying is better than a little dependency.
Syscall must always be guarded with build tags.
Cgo must always be guarded with build tags.
Go Proverbs 3/4
Cgo is not Go.
With the unsafe package there are no guarantees.
Clear is better than clever.
Re ection is never clear.
Errors are values.
Go Proverbs 4/4
Don't just check errors, handle them gracefully.
Design the architecture, name the components, document the details.
Documentation is for users.
Don't panic.
Recap
9. First-class functions
10. Fully quali ed imports
11. Static typing with type inference
12. Speed
13. Metaprogramming
14. Static linking
15. Cross-compilation
16. Go Proverbs
Thank you
Rodolfo Carvalho
Red Hat
rhcarvalho@gmail.com
http://rodolfocarvalho.net

More Related Content

PDF
A Tour of Go - Workshop
PDF
The Go features I can't live without
PDF
Introduction to go language programming
PDF
Introduction to Go language
PPT
Go lang introduction
PDF
Golang
PDF
To GO or not to GO
PDF
10 reasons to be excited about go
A Tour of Go - Workshop
The Go features I can't live without
Introduction to go language programming
Introduction to Go language
Go lang introduction
Golang
To GO or not to GO
10 reasons to be excited about go

What's hot (20)

PDF
How to build SDKs in Go
PDF
Programming with Python - Adv.
PDF
Go language presentation
PDF
Using TypeScript at Dashlane
PDF
A Recovering Java Developer Learns to Go
PDF
Introduction to Clime
PDF
Decision making - for loop , nested loop ,if-else statements , switch in goph...
PDF
Learning Python from Data
PDF
Not Your Fathers C - C Application Development In 2016
PDF
Static Analysis in Go
PDF
Low latency Logging (BrightonPHP - 18th Nov 2013)
PDF
Python debugging techniques
PDF
Writing Fast Code (JP) - PyCon JP 2015
PDF
Introduction to python
PDF
FTD JVM Internals
PDF
Dive into Pinkoi 2013
PDF
What is Python Lambda Function? Python Tutorial | Edureka
PPT
GO programming language
PPTX
Clonedigger-Python
How to build SDKs in Go
Programming with Python - Adv.
Go language presentation
Using TypeScript at Dashlane
A Recovering Java Developer Learns to Go
Introduction to Clime
Decision making - for loop , nested loop ,if-else statements , switch in goph...
Learning Python from Data
Not Your Fathers C - C Application Development In 2016
Static Analysis in Go
Low latency Logging (BrightonPHP - 18th Nov 2013)
Python debugging techniques
Writing Fast Code (JP) - PyCon JP 2015
Introduction to python
FTD JVM Internals
Dive into Pinkoi 2013
What is Python Lambda Function? Python Tutorial | Edureka
GO programming language
Clonedigger-Python
Ad

Similar to The Go features I can't live without, 2nd round (20)

PPTX
Ready, set, go! An introduction to the Go programming language
PPTX
Introduction to go lang
PPTX
Scaling applications with go
PDF
Introduction to Google's Go programming language
PDF
Mender.io | Develop embedded applications faster | Comparing C and Golang
PDF
Why you should care about Go (Golang)
PPTX
Comparing C and Go
PPTX
The GO Language : From Beginners to Gophers
PDF
Going All-In With Go For CLI Apps
PDF
Getting Started with Go
PDF
Inroduction to golang
PDF
Introduction to Go
PDF
Introduction to Programming in Go
PDF
Go - Where it's going and why you should pay attention.
PDF
Features of go
PDF
Go. why it goes v2
PDF
Beyond the Hype: 4 Years of Go in Production
PPTX
Go programming language
PDF
Golang workshop
PPT
A First Look at Google's Go Programming Language
Ready, set, go! An introduction to the Go programming language
Introduction to go lang
Scaling applications with go
Introduction to Google's Go programming language
Mender.io | Develop embedded applications faster | Comparing C and Golang
Why you should care about Go (Golang)
Comparing C and Go
The GO Language : From Beginners to Gophers
Going All-In With Go For CLI Apps
Getting Started with Go
Inroduction to golang
Introduction to Go
Introduction to Programming in Go
Go - Where it's going and why you should pay attention.
Features of go
Go. why it goes v2
Beyond the Hype: 4 Years of Go in Production
Go programming language
Golang workshop
A First Look at Google's Go Programming Language
Ad

More from Rodolfo Carvalho (20)

PDF
Go 1.10 Release Party - PDX Go
PDF
OpenShift Overview - Red Hat Open School 2017
PDF
OpenShift Overview - Red Hat Open House 2017
PDF
Automation with Ansible and Containers
PDF
Go 1.8 Release Party
PDF
Building and Deploying containerized Python Apps in the Cloud
PDF
Python deployments on OpenShift 3
PDF
Composing WSGI apps and spellchecking it all
PDF
Pykonik Coding Dojo
PDF
Concurrency in Python4k
PDF
Coding Kwoon
PDF
Python in 15 minutes
PDF
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...
PDF
Redes livres de escala
PDF
Redes livres de escala
PDF
Content Delivery Networks
PDF
TDD do seu jeito
PDF
Intro Dojo Rio Python Campus
PDF
Intro Dojo Rio
Go 1.10 Release Party - PDX Go
OpenShift Overview - Red Hat Open School 2017
OpenShift Overview - Red Hat Open House 2017
Automation with Ansible and Containers
Go 1.8 Release Party
Building and Deploying containerized Python Apps in the Cloud
Python deployments on OpenShift 3
Composing WSGI apps and spellchecking it all
Pykonik Coding Dojo
Concurrency in Python4k
Coding Kwoon
Python in 15 minutes
O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding D...
Redes livres de escala
Redes livres de escala
Content Delivery Networks
TDD do seu jeito
Intro Dojo Rio Python Campus
Intro Dojo Rio

Recently uploaded (20)

PPTX
Patient Appointment Booking in Odoo with online payment
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PDF
Download FL Studio Crack Latest version 2025 ?
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Complete Guide to Website Development in Malaysia for SMEs
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
iTop VPN Crack Latest Version Full Key 2025
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Patient Appointment Booking in Odoo with online payment
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
17 Powerful Integrations Your Next-Gen MLM Software Needs
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
Download FL Studio Crack Latest version 2025 ?
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
Autodesk AutoCAD Crack Free Download 2025
Operating system designcfffgfgggggggvggggggggg
Complete Guide to Website Development in Malaysia for SMEs
Oracle Fusion HCM Cloud Demo for Beginners
Design an Analysis of Algorithms I-SECS-1021-03
iTop VPN Crack Latest Version Full Key 2025
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free

The Go features I can't live without, 2nd round