Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Golang for PHP
developers
A practical introduction 



by Richard Tuin
Who am I?
• @Richard_Tuin

• Doing mostly PHP

• Last 5 years at Enrise

• First day @ Egeniq!

• PHPBenelux

• Creator of...
About the presentation
WHAT IS
GOLANG
GOLANG 

USE CASE
GETTING
STARTED
What is Golang?
• Origin of Golang

• Hello World example

• Language features
The origin of Golang
What problems
does it solve?
The origin of Golang
Hello World example
package main
import "fmt"
func main () {
world := "DPC 2017"
fmt.Println("Hello", world)
// Hello DPC ...
Application lifecycles

(simplified)
What makes Golang
Load file(s)
Compiles
Execute
PHP application lifecycle
What makes Golang
Load file(s)
Compiles
Execute
Request
Response
Go application lifecycle
What makes Golang
Load file(s)
Compiles
Execute
Request Response
Compilation into binary
Language features
• Stuff that mostly equal to other languages

• Scalar types (string, int, bool)

• Too complex types (slices)
Not going ov...
Types: Maps
Language features
daysMap := make(map[string]int)
daysMap["April"] = 30
daysMap["May"] = 31
daysMap["May"] // ...
Types: Structures
Language features
type Message struct {
Category string
Content string
}
msg := Message{
Category: "Gree...
Types: Structures 2
Language features
type Cow struct {}
func (c Cow) Greet() string {
return "Moo"
}
c := Cow{}
c.Greet()...
Types: Structures 3
Language features
type Duck struct {}
func (d Duck) Greet() string {
return "Quack"
}
Types: Interfaces
Language features
type Greeter interface {
Greet() string
}
type Cow struct {} // Greet()
type Duck stru...
Types: Interfaces 2
Language features
func SayHello(g Greeter) {
fmt.Println("Greeter says:", g.Greet())
}
SayHello(c) // ...
Types: Interfaces 3
Language features
./file.go:67:
cannot use t (type Tree) as type Greeter
in argument to SayHello:
Tree...
Go-routines
Language features
func WaitShort() {
time.Sleep(500 * time.Millisecond)
}
func WaitLong() {
time.Sleep(1000 * ...
Go-routines 2
Language features
WaitLong()
WaitShort()
WaitLong()
Time
WaitShort()
Go-routines 3
Language features
go WaitLong()
WaitShort()
WaitLong()
Time
WaitShort()
Channels
Language features
messages := make(chan string)
Example from: https://gobyexample.com/channels
go func() { messag...
Golang use case
• HTTP Proxy 

• Web Interface

• to inspect server-to-server communication

• Built to run on production
What is Camilla ...
What is Camilla Proxy?
Golang use case
How does it work?
Golang use case
YOUR APP GITHUB API
HTTP
How does it work?
Golang use case
YOUR APP GITHUB API
CAMILLA PROXY
WEB INTERFACE
PROXY
CLIENTCLIENT CLIENT
Concurrency in Camilla Proxy
Golang use case
YOUR APP GITHUB API
CAMILLA PROXY
WEB INTERFACE
PROXY
CLIENTCLIENT CLIENT
Go! Getting started
• Your Go environment

• Running your first program

• Using external packages

• Go programmer’s toolb...
Your Go environment
Go! Getting started
Extract Go package into /usr/local/go — or — C:GoSTEP 1
Create the directory ~/go....
Running your first program
Go! Getting started
// file: ~/go/src/hello/hello.go
package main
import "fmt"
func main () {
wo...
Running your first program
Go! Getting started
> cd ~/go/src/hello
> go run hello.go
Hello DPC 2017
Building your first program
Go! Getting started
> cd ~/go/src/hello
> go build
> ./hello
Hello DPC 2017
Using external packages
Go! Getting started
> go get [package name]
> go get github.com/sirupsen/logrus
This will download...
Using external packages
Go! Getting started
package main
import "github.com/sirupsen/logrus"
func main() {
logrus.Info("Lo...
• Editors and IDE’s

• Composer for Golang

• Testing Golang applications
Go programmer’s toolbox
Go! Getting started
Editors and IDE’s
Go programmer’s toolbox
IDE’sEditors
•Sublime Text
•Atom
•vIM
•…
• Gogland (Jetbrains)
• PHPstorm w/ Go ...
Composer for Golang
Go programmer’s toolbox
G l i de
Testing
Go! Getting started
// file: ~/go/src/hello/hello_test.go
import (
"testing"
"github.com/stretchr/testify/assert"
...
I hope you are now
excited about Go!
Questions?
Get in touch
• @Richard_Tuin

• me@rtuin.nl

• rtuin @ phpnl Slack
Upcoming SlideShare
Loading in …5
×

Golang for PHP programmers: A practical introduction

PHP and Golang complement each other in a great way. In this presentation you will first learn how Golang is different from PHP, then we will look at its use-cases with some examples, and finally we explore some important best practices. After this presentation you will know when Golang could be a suitable tool for the job.

  • Be the first to comment

Golang for PHP programmers: A practical introduction

  1. 1. Golang for PHP developers A practical introduction 
 by Richard Tuin
  2. 2. Who am I? • @Richard_Tuin • Doing mostly PHP • Last 5 years at Enrise • First day @ Egeniq! • PHPBenelux • Creator of Camilla Proxy
  3. 3. About the presentation WHAT IS GOLANG GOLANG 
 USE CASE GETTING STARTED
  4. 4. What is Golang? • Origin of Golang • Hello World example • Language features
  5. 5. The origin of Golang
  6. 6. What problems does it solve? The origin of Golang
  7. 7. Hello World example package main import "fmt" func main () { world := "DPC 2017" fmt.Println("Hello", world) // Hello DPC 2017 }
  8. 8. Application lifecycles
 (simplified) What makes Golang Load file(s) Compiles Execute
  9. 9. PHP application lifecycle What makes Golang Load file(s) Compiles Execute Request Response
  10. 10. Go application lifecycle What makes Golang Load file(s) Compiles Execute Request Response Compilation into binary
  11. 11. Language features
  12. 12. • Stuff that mostly equal to other languages • Scalar types (string, int, bool) • Too complex types (slices) Not going over… Language features
  13. 13. Types: Maps Language features daysMap := make(map[string]int) daysMap["April"] = 30 daysMap["May"] = 31 daysMap["May"] // 31 fmt.Println(daysMap) // map[April:30 May:31]
  14. 14. Types: Structures Language features type Message struct { Category string Content string } msg := Message{ Category: "Greeting", Content: "Hello DPC!", } msg.Content // Hello DPC!
  15. 15. Types: Structures 2 Language features type Cow struct {} func (c Cow) Greet() string { return "Moo" } c := Cow{} c.Greet() // Cow says: Moo
  16. 16. Types: Structures 3 Language features type Duck struct {} func (d Duck) Greet() string { return "Quack" }
  17. 17. Types: Interfaces Language features type Greeter interface { Greet() string } type Cow struct {} // Greet() type Duck struct {} // Greet() type Tree struct {} // Doesn’t Greet()
  18. 18. Types: Interfaces 2 Language features func SayHello(g Greeter) { fmt.Println("Greeter says:", g.Greet()) } SayHello(c) // Greeter says: Moo SayHello(d) // Greeter says: Quack SayHello(t) // Compiler error c := Cow{} d := Duck{} t := Tree{}
  19. 19. Types: Interfaces 3 Language features ./file.go:67: cannot use t (type Tree) as type Greeter in argument to SayHello: Tree does not implement Greeter (missing Greet method)
  20. 20. Go-routines Language features func WaitShort() { time.Sleep(500 * time.Millisecond) } func WaitLong() { time.Sleep(1000 * time.Millisecond) }
  21. 21. Go-routines 2 Language features WaitLong() WaitShort() WaitLong() Time WaitShort()
  22. 22. Go-routines 3 Language features go WaitLong() WaitShort() WaitLong() Time WaitShort()
  23. 23. Channels Language features messages := make(chan string) Example from: https://gobyexample.com/channels go func() { messages <- "ping" }() incoming := <-messages fmt.Println(incoming)
  24. 24. Golang use case
  25. 25. • HTTP Proxy • Web Interface • to inspect server-to-server communication • Built to run on production What is Camilla Proxy? Golang use case
  26. 26. What is Camilla Proxy? Golang use case
  27. 27. How does it work? Golang use case YOUR APP GITHUB API HTTP
  28. 28. How does it work? Golang use case YOUR APP GITHUB API CAMILLA PROXY WEB INTERFACE PROXY CLIENTCLIENT CLIENT
  29. 29. Concurrency in Camilla Proxy Golang use case YOUR APP GITHUB API CAMILLA PROXY WEB INTERFACE PROXY CLIENTCLIENT CLIENT
  30. 30. Go! Getting started • Your Go environment • Running your first program • Using external packages • Go programmer’s toolbox
  31. 31. Your Go environment Go! Getting started Extract Go package into /usr/local/go — or — C:GoSTEP 1 Create the directory ~/go. This is your workspaceSTEP 2
  32. 32. Running your first program Go! Getting started // file: ~/go/src/hello/hello.go package main import "fmt" func main () { world := "DPC 2017" fmt.Println("Hello", world) }
  33. 33. Running your first program Go! Getting started > cd ~/go/src/hello > go run hello.go Hello DPC 2017
  34. 34. Building your first program Go! Getting started > cd ~/go/src/hello > go build > ./hello Hello DPC 2017
  35. 35. Using external packages Go! Getting started > go get [package name] > go get github.com/sirupsen/logrus This will download the package into: ~/go/src/github.com/sirupsen/logrus
  36. 36. Using external packages Go! Getting started package main import "github.com/sirupsen/logrus" func main() { logrus.Info("Log using logrus") }
  37. 37. • Editors and IDE’s • Composer for Golang • Testing Golang applications Go programmer’s toolbox Go! Getting started
  38. 38. Editors and IDE’s Go programmer’s toolbox IDE’sEditors •Sublime Text •Atom •vIM •… • Gogland (Jetbrains) • PHPstorm w/ Go plugin • VS Code
  39. 39. Composer for Golang Go programmer’s toolbox G l i de
  40. 40. Testing Go! Getting started // file: ~/go/src/hello/hello_test.go import ( "testing" "github.com/stretchr/testify/assert" ) func TestTwoVariables(t *testing.T) { assert.NotEqual(t, 1, 2) } > go test PASS ok hello 0.010s
  41. 41. I hope you are now excited about Go!
  42. 42. Questions?
  43. 43. Get in touch • @Richard_Tuin • me@rtuin.nl • rtuin @ phpnl Slack

×