SlideShare a Scribd company logo
Learning Go for Perl
Programmers
Fred Moyer
SF Perl Mongers, 06082016
The Go Programming Language
Developed at Google circa 2007
Goroutines (threads)
Channels (queues)
One way to format code (gofmt)
Hello SF.pm!
package main
import "fmt"
func main() {
fmt.Println("Hello, サンフランシスコのPerlモンガー")
}
Hash || Object => Struct
type Person struct {
id int // lowercase fields are not exported
email string // note no commas after variable type
Name string
HeightCentimeters float32 // camelCase convention
IsAlive bool // true or false, defaults to false
}
Hash || Object => Struct
var famousActor = Person{
id: 1,
email: "jeff@goldblum.org",
Name: "Jeff Goldblum", // no single quotes
HeightCentimeters: 193.5,
IsAlive: true,
}
Hash || Object => Struct
// we all knew this day would come
// perhaps in Independence Day: Resurgence?
// use the dot notation to make it happen
famousActor.IsAlive = false
Array => Slice
var tallActors []string
tallActors = append(tallActors, “Dolph Lundgren”)
tallActors[0] = “Richard Kiel”
tallActors[1] = “Chevy Chase” // error: index out of range
tallActors = append(tallActors, “Vince Vaughn”)
tallActorsCopy := make([]string, len(tallActors))
copy(tallActorsCopy, tallActors)
Array => Array
// not used nearly as much as slices
var tenIntegers [10]int
fiveIntegers := [5]int{1,2,3,4,5}
lastThreeIntegers := fiveIntegers[2:] // outputs 3,4,5
firstTwoIntegers := fiveIntegers[:2] // outputs 1,2
Hash => Map
countries := make(map[string]int)
countries[“Canada”] = 1
countries[“US”] = 2
canadaId = countries[“Canada”] // 1
delete(countries, “US”)
countries := map[string]int{“Canada”: 1, “US”: 2}
countryId, exists := countries[“Atlantis”] // exists == nil
Loops
for i:= 0; i < 10; i++ {
fmt.Printf(“We’re going to 11”, i+1)
}
for i, actor := range []string{“Van Dam”, “Liam Neeson”} {
fmt.Printf(“#%d is %v”, i, actor) // %v default format
}
for _, actor := ... // _ ignores the loop iterator value
$@ => err
bytesRead, err := w.Write([]byte{“data written to client”})
if err != nil {
log.WithField("err", err).Error("write to client failed")
}
var err error // built in error type
err.Error() // format error as a string
use => import
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
)
sub foo => func foo (bar string, boo int)
func httpEndpoint(world string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r
*http.Request) {
bytesRead, _ := w.Write([]byte(
fmt.Sprintf(“Hello %v!”, world)))
return
}
}
t/test.t => main_test.go
main.go # file containing your code, ‘go run main.go’
main_test.go # unit test for main.go
go test # runs main_test.go, executes all tests
func TestSomething(t *testing.T) {
// do some stuff
if err != nil { t.Errorf("test failed %v", err) }
}
perldoc => godoc
$ godoc fmt
use 'godoc cmd/fmt' for documentation on the fmt command
PACKAGE DOCUMENTATION
package fmt
import "fmt"
...
perldoc => godoc
$ godoc fmt Sprintf
use 'godoc cmd/fmt' for documentation on the fmt command
func Sprintf(format string, a ...interface{}) string
Sprintf formats according to a format specifier and
returns the
resulting string.
CPAN => https://golang.org/pkg/
cpanminus => go get
go get github.com/quipo/statsd
$ ls gocode/src/github.com/quipo/statsd/
LICENSE bufferedclient_test.go event
README.md client.go interface.go
bufferedclient.go client_test.go noopclient.go
PERL5LIB => GOPATH
$ echo $GOPATH
/Users/phred/gocode
$ ls gocode/
bin pkg src
$ ls gocode/src/
github.com glenda golang.org
? => go build
$ pwd
~/gocode/src/myorg/myapp
$ ls
main.go main_test.go
$ go build; ls
main.go main_test.go myapp
./myapp # binary executable you can relocate to same arch
queues => channels
c := make(chan string) // queue for string data type
hello := “Hello SF.pm”
c <- hello
// ...
fmt.Println(<-c) // prints “Hello.SF.pm”
bufferedChannel := make(chan string, 2) // buffers 2 strings
// channels are first class citizens of Go
threads => goroutines
hello := “Hello SF.pm”
go myFunc(hello)
func myFunc(myString string) {
fmt.Println(myString)
}
// spawns an asynchronous thread which executes a function
// goroutines are first class citizens of Go
Tour of Golang web places
https://golang.org/
https://tour.golang.org/welcome/1
https://golang.org/pkg/
https://groups.google.com/forum/#!forum/golang-nuts
Questions?

More Related Content

PDF
Happy Go Programming Part 1
PDF
Go for Object Oriented Programmers or Object Oriented Programming without Obj...
PDF
7 Common mistakes in Go and when to avoid them
PDF
7 Common Mistakes in Go (2015)
PDF
Go Lang Tutorial
ODP
Hands on Session on Python
PPTX
Go Language Hands-on Workshop Material
PDF
Introduction to go language programming
Happy Go Programming Part 1
Go for Object Oriented Programmers or Object Oriented Programming without Obj...
7 Common mistakes in Go and when to avoid them
7 Common Mistakes in Go (2015)
Go Lang Tutorial
Hands on Session on Python
Go Language Hands-on Workshop Material
Introduction to go language programming

What's hot (20)

PDF
Introduction to Programming in Go
PDF
From 0 to mine sweeper in pyside
PPTX
Golang iran - tutorial go programming language - Preliminary
PDF
Happy Go Programming
PDF
Vim Script Programming
ODP
Getting groovy (ODP)
PDF
How to develop a rich terminal UI application
PDF
Golangにおける端末制御 リッチなターミナルUIの実現方法
PPT
Unix And C
ODP
An Intro to Python in 30 minutes
PDF
Getting Started with Go
PDF
Defer, Panic, Recover
PDF
Painless Data Storage with MongoDB & Go
PDF
Golang
PDF
C: A Humbling Language
PPTX
Linux basic1&amp;2
ODP
Отладка в GDB
PDF
PyCon 2013 : Scripting to PyPi to GitHub and More
PDF
Coding in GO - GDG SL - NSBM
PDF
Introduction to python
Introduction to Programming in Go
From 0 to mine sweeper in pyside
Golang iran - tutorial go programming language - Preliminary
Happy Go Programming
Vim Script Programming
Getting groovy (ODP)
How to develop a rich terminal UI application
Golangにおける端末制御 リッチなターミナルUIの実現方法
Unix And C
An Intro to Python in 30 minutes
Getting Started with Go
Defer, Panic, Recover
Painless Data Storage with MongoDB & Go
Golang
C: A Humbling Language
Linux basic1&amp;2
Отладка в GDB
PyCon 2013 : Scripting to PyPi to GitHub and More
Coding in GO - GDG SL - NSBM
Introduction to python
Ad

Viewers also liked (7)

PPT
GO programming language
PPTX
Как устроен поиск (Андрей Аксёнов)
PDF
Анатомия веб сервиса (HighLoad-2014)
PDF
Консольные приложения на Go
PDF
Клиентские приложения под нагрузкой (HighLoad 2014)
PDF
Fuzzing: The New Unit Testing
PDF
Develop Android app using Golang
GO programming language
Как устроен поиск (Андрей Аксёнов)
Анатомия веб сервиса (HighLoad-2014)
Консольные приложения на Go
Клиентские приложения под нагрузкой (HighLoad 2014)
Fuzzing: The New Unit Testing
Develop Android app using Golang
Ad

Similar to Learning go for perl programmers (20)

PPTX
Golang basics for Java developers - Part 1
PDF
Let's Go-lang
PDF
Go serving: Building server app with go
PDF
Golang workshop
PPT
Fantom on the JVM Devoxx09 BOF
PDF
Go for Rubyists
PPTX
Introduction to Go
PPT
2007 09 10 Fzi Training Groovy Grails V Ws
KEY
PPTX
golang_getting_started.pptx
PDF
OSCON2014 : Quick Introduction to System Tools Programming with Go
PPT
Unit 8
PDF
A Tour of Go - Workshop
KEY
Sbaw091006
KEY
Beauty and Power of Go
PDF
Formatting ForThe Masses
PPTX
Building a friendly .NET SDK to connect to Space
PDF
Geeks Anonymes - Le langage Go
PDF
Fantom - Programming Language for JVM, CLR, and Javascript
PPTX
NodeJs
Golang basics for Java developers - Part 1
Let's Go-lang
Go serving: Building server app with go
Golang workshop
Fantom on the JVM Devoxx09 BOF
Go for Rubyists
Introduction to Go
2007 09 10 Fzi Training Groovy Grails V Ws
golang_getting_started.pptx
OSCON2014 : Quick Introduction to System Tools Programming with Go
Unit 8
A Tour of Go - Workshop
Sbaw091006
Beauty and Power of Go
Formatting ForThe Masses
Building a friendly .NET SDK to connect to Space
Geeks Anonymes - Le langage Go
Fantom - Programming Language for JVM, CLR, and Javascript
NodeJs

More from Fred Moyer (20)

PDF
Reliable observability at scale: Error Budgets for 1,000+
PPTX
Practical service level objectives with error budgeting
PPTX
SREcon americas 2019 - Latency SLOs Done Right
PPTX
Scale17x - Latency SLOs Done Right
PPTX
Latency SLOs Done Right
PPTX
Latency SLOs done right
PPTX
Comprehensive Container Based Service Monitoring with Kubernetes and Istio
PPTX
Comprehensive container based service monitoring with kubernetes and istio
PPTX
Effective management of high volume numeric data with histograms
PPTX
Statistics for dummies
PPTX
GrafanaCon EU 2018
PDF
Fredmoyer postgresopen 2017
PPTX
Better service monitoring through histograms sv perl 09012016
PPTX
Better service monitoring through histograms
PPTX
The Breakup - Logically Sharding a Growing PostgreSQL Database
PDF
Surge 2012 fred_moyer_lightning
PDF
Qpsmtpd
PDF
Apache Dispatch
PDF
Ball Of Mud Yapc 2008
KEY
Data::FormValidator Simplified
Reliable observability at scale: Error Budgets for 1,000+
Practical service level objectives with error budgeting
SREcon americas 2019 - Latency SLOs Done Right
Scale17x - Latency SLOs Done Right
Latency SLOs Done Right
Latency SLOs done right
Comprehensive Container Based Service Monitoring with Kubernetes and Istio
Comprehensive container based service monitoring with kubernetes and istio
Effective management of high volume numeric data with histograms
Statistics for dummies
GrafanaCon EU 2018
Fredmoyer postgresopen 2017
Better service monitoring through histograms sv perl 09012016
Better service monitoring through histograms
The Breakup - Logically Sharding a Growing PostgreSQL Database
Surge 2012 fred_moyer_lightning
Qpsmtpd
Apache Dispatch
Ball Of Mud Yapc 2008
Data::FormValidator Simplified

Recently uploaded (20)

PDF
AI in Product Development-omnex systems
PPTX
Introduction to Artificial Intelligence
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
System and Network Administraation Chapter 3
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
System and Network Administration Chapter 2
PDF
PTS Company Brochure 2025 (1).pdf.......
PPT
Introduction Database Management System for Course Database
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Nekopoi APK 2025 free lastest update
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
AI in Product Development-omnex systems
Introduction to Artificial Intelligence
Wondershare Filmora 15 Crack With Activation Key [2025
System and Network Administraation Chapter 3
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Operating system designcfffgfgggggggvggggggggg
System and Network Administration Chapter 2
PTS Company Brochure 2025 (1).pdf.......
Introduction Database Management System for Course Database
Upgrade and Innovation Strategies for SAP ERP Customers
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
CHAPTER 2 - PM Management and IT Context
How Creative Agencies Leverage Project Management Software.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
Nekopoi APK 2025 free lastest update
Navsoft: AI-Powered Business Solutions & Custom Software Development
Odoo Companies in India – Driving Business Transformation.pdf
ai tools demonstartion for schools and inter college
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf

Learning go for perl programmers

  • 1. Learning Go for Perl Programmers Fred Moyer SF Perl Mongers, 06082016
  • 2. The Go Programming Language Developed at Google circa 2007 Goroutines (threads) Channels (queues) One way to format code (gofmt)
  • 3. Hello SF.pm! package main import "fmt" func main() { fmt.Println("Hello, サンフランシスコのPerlモンガー") }
  • 4. Hash || Object => Struct type Person struct { id int // lowercase fields are not exported email string // note no commas after variable type Name string HeightCentimeters float32 // camelCase convention IsAlive bool // true or false, defaults to false }
  • 5. Hash || Object => Struct var famousActor = Person{ id: 1, email: "[email protected]", Name: "Jeff Goldblum", // no single quotes HeightCentimeters: 193.5, IsAlive: true, }
  • 6. Hash || Object => Struct // we all knew this day would come // perhaps in Independence Day: Resurgence? // use the dot notation to make it happen famousActor.IsAlive = false
  • 7. Array => Slice var tallActors []string tallActors = append(tallActors, “Dolph Lundgren”) tallActors[0] = “Richard Kiel” tallActors[1] = “Chevy Chase” // error: index out of range tallActors = append(tallActors, “Vince Vaughn”) tallActorsCopy := make([]string, len(tallActors)) copy(tallActorsCopy, tallActors)
  • 8. Array => Array // not used nearly as much as slices var tenIntegers [10]int fiveIntegers := [5]int{1,2,3,4,5} lastThreeIntegers := fiveIntegers[2:] // outputs 3,4,5 firstTwoIntegers := fiveIntegers[:2] // outputs 1,2
  • 9. Hash => Map countries := make(map[string]int) countries[“Canada”] = 1 countries[“US”] = 2 canadaId = countries[“Canada”] // 1 delete(countries, “US”) countries := map[string]int{“Canada”: 1, “US”: 2} countryId, exists := countries[“Atlantis”] // exists == nil
  • 10. Loops for i:= 0; i < 10; i++ { fmt.Printf(“We’re going to 11”, i+1) } for i, actor := range []string{“Van Dam”, “Liam Neeson”} { fmt.Printf(“#%d is %v”, i, actor) // %v default format } for _, actor := ... // _ ignores the loop iterator value
  • 11. $@ => err bytesRead, err := w.Write([]byte{“data written to client”}) if err != nil { log.WithField("err", err).Error("write to client failed") } var err error // built in error type err.Error() // format error as a string
  • 12. use => import import ( "database/sql" "encoding/json" "flag" "fmt" )
  • 13. sub foo => func foo (bar string, boo int) func httpEndpoint(world string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { bytesRead, _ := w.Write([]byte( fmt.Sprintf(“Hello %v!”, world))) return } }
  • 14. t/test.t => main_test.go main.go # file containing your code, ‘go run main.go’ main_test.go # unit test for main.go go test # runs main_test.go, executes all tests func TestSomething(t *testing.T) { // do some stuff if err != nil { t.Errorf("test failed %v", err) } }
  • 15. perldoc => godoc $ godoc fmt use 'godoc cmd/fmt' for documentation on the fmt command PACKAGE DOCUMENTATION package fmt import "fmt" ...
  • 16. perldoc => godoc $ godoc fmt Sprintf use 'godoc cmd/fmt' for documentation on the fmt command func Sprintf(format string, a ...interface{}) string Sprintf formats according to a format specifier and returns the resulting string.
  • 18. cpanminus => go get go get github.com/quipo/statsd $ ls gocode/src/github.com/quipo/statsd/ LICENSE bufferedclient_test.go event README.md client.go interface.go bufferedclient.go client_test.go noopclient.go
  • 19. PERL5LIB => GOPATH $ echo $GOPATH /Users/phred/gocode $ ls gocode/ bin pkg src $ ls gocode/src/ github.com glenda golang.org
  • 20. ? => go build $ pwd ~/gocode/src/myorg/myapp $ ls main.go main_test.go $ go build; ls main.go main_test.go myapp ./myapp # binary executable you can relocate to same arch
  • 21. queues => channels c := make(chan string) // queue for string data type hello := “Hello SF.pm” c <- hello // ... fmt.Println(<-c) // prints “Hello.SF.pm” bufferedChannel := make(chan string, 2) // buffers 2 strings // channels are first class citizens of Go
  • 22. threads => goroutines hello := “Hello SF.pm” go myFunc(hello) func myFunc(myString string) { fmt.Println(myString) } // spawns an asynchronous thread which executes a function // goroutines are first class citizens of Go
  • 23. Tour of Golang web places https://golang.org/ https://tour.golang.org/welcome/1 https://golang.org/pkg/ https://groups.google.com/forum/#!forum/golang-nuts