Go for PHP devs
rethinking long running
tasks
#ViennaPHP
Slide 2
Slide 2 text
Johannes Pichler
• Web Developer since 2006
• working @ karriere.at
• doing backend stuff in PHP
2
Slide 3
Slide 3 text
karriere.at
• biggest job platform in Austria
3
Slide 4
Slide 4 text
karriere.at - devs
4
Slide 5
Slide 5 text
Long running tasks in
PHP?
5
Slide 6
Slide 6 text
Problems with long running tasks
• memorylimit and maxexecution_time limitations
• what happens if the process dies?
6
Slide 7
Slide 7 text
Potential solutions
• set limits to maximum !
• limit execution time and reschedule task
• use a more appropriate programming language
7
Slide 8
Slide 8 text
Considering other languages
• (enterprise) Java
• python, ruby …
• C/C++ !
• GOlang
8
Slide 9
Slide 9 text
Get GOing
9
Slide 10
Slide 10 text
The basics - package declaration
package main
import "fmt"
func main() {
fmt.Printf("Hello World!")
}
10
Slide 11
Slide 11 text
The basics - imports
package main
import "fmt"
func main() {
fmt.Printf("Hello World!")
}
11
Slide 12
Slide 12 text
The basics - main function
package main
import "fmt"
func main() {
fmt.Printf("Hello World!")
}
12
Slide 13
Slide 13 text
Variables
var abc string = "a string variable"
var number int // number = 0
var emptyString string // emptyString = ""
short := "a string variable" // var short string = "a string variable"
13
Slide 14
Slide 14 text
Variables
var abc string = "a string variable"
var number int // number = 0
var emptyString string // emptyString = ""
short := "a string variable" // var short string = "a string variable"
14
Slide 15
Slide 15 text
Variables
var abc string = "a string variable"
var number int // number = 0
var emptyString string // emptyString = ""
short := "a string variable" // var short string = "a string variable"
15
Slide 16
Slide 16 text
Arrays
// have a fixed size
types := [5]int // [0 0 0 0 0]
// values can be changed and retrieve
types[2] = 3 // [0 0 3 0 0]
val := types[2] // 3
// can be initialized on delaration
types := [5]int{1, 2, 3, 4, 5} // [1 2 3 4 5]
// can have more dimensions
more := [2][3]int
16
Slide 17
Slide 17 text
Arrays
// have a fixed size
types := [5]int // [0 0 0 0 0]
// values can be changed and retrieve
types[2] = 3 // [0 0 3 0 0]
val := types[2] // 3
// can be initialized on delaration
types := [5]int{1, 2, 3, 4, 5} // [1 2 3 4 5]
// can have more dimensions
more := [2][3]int
17
Slide 18
Slide 18 text
Arrays
// have a fixed size
types := [5]int // [0 0 0 0 0]
// values can be changed and retrieved
types[2] = 3 // [0 0 3 0 0]
val := types[2] // 3
// can be initialized on delaration
types := [5]int{1, 2, 3, 4, 5} // [1 2 3 4 5]
// can have more dimensions
more := [2][3]int
18
Slide 19
Slide 19 text
Arrays
// have a fixed size
types := [5]int // [0 0 0 0 0]
// values can be changed and retrieve
types[2] = 3 // [0 0 3 0 0]
val := types[2] // 3
// can be initialized on delaration
types := [5]int{1, 2, 3, 4, 5} // [1 2 3 4 5]
// can have more dimensions
more := [2][3]int
19
Interfaces
func print(a animal) {
fmt.Println(a.color())
}
func main() {
c1 := cat{"Kitty"}
c2 := cat{"Miau"}
m := mouse{"Pinky"}
print(c1) // black
print(c2) // white
print(m) // grey
}
34
Slide 35
Slide 35 text
Goroutines
• concurrent execution of code
• synchronization needed for shared resources (mutex, lock)
35
Slide 36
Slide 36 text
Goroutines
func doSomeWork() {
for i := 0; i < 5; i++ {
fmt.Println("Work index:", i)
}
}
func main() {
go doSomeWork()
// continue with main thread flow
}
36
Slide 37
Slide 37 text
Channels
• used for communication between goroutines
• sender sends messages, receiver reads from channel
• channels are basically blocking except they are buffered
37
Slide 38
Slide 38 text
Channels
// create a channel
messages := make(chan string)
// send a message
messages <- "a message"
// read a message
msg := <- messages
38
Slide 39
Slide 39 text
Channels
// create a channel
messages := make(chan string)
// send a message
messages <- "a message"
// read a message
msg := <- messages
39
Slide 40
Slide 40 text
Channels
// create a channel
messages := make(chan string)
// send a message
messages <- "a message"
// read a message
msg := <- messages
40
Slide 41
Slide 41 text
Demo Time #2
Goroutines
41
Slide 42
Slide 42 text
Testing in Go
import "testing"
...
func TestTestName(t *testing.T) {
// test code
}
42
Slide 43
Slide 43 text
Testing in Go
1. define your test set
2. iterate over your test set and validate the function under test
3. fail in case of an error
43
Slide 44
Slide 44 text
Demo Time #3
Testing the
calculator
44
Slide 45
Slide 45 text
Tooling for Go
• code formating
• linting
• testing
• benchmarking
• documentation
• profiling
45
Slide 46
Slide 46 text
my golang favorites
• strong type system
• error handling/multiple return values
• implicit interfaces
• built-in tooling
• concurrency
• speed
46
Slide 47
Slide 47 text
Helpful resources
• A Tour of Go
https://tour.golang.org
• Go by Example
https://gobyexample.com
• List of Go Books
https://github.com/dariubs/GoBooks
47