Go for PHP devs

Go for PHP devs

Ever struggled with a long running task in PHP? Maybe it is time to consider a more appropriate programming language like golang? In this talk I give an introduction to golang

E44317a06f6ff5fec214d3f54f7ba432?s=128

Johannes Pichler

June 20, 2017
Tweet

Transcript

  1. Go for PHP devs rethinking long running tasks #ViennaPHP

  2. Johannes Pichler • Web Developer since 2006 • working @

    karriere.at • doing backend stuff in PHP 2
  3. karriere.at • biggest job platform in Austria 3

  4. karriere.at - devs 4

  5. Long running tasks in PHP? 5

  6. Problems with long running tasks • memorylimit and maxexecution_time limitations

    • what happens if the process dies? 6
  7. Potential solutions • set limits to maximum ! • limit

    execution time and reschedule task • use a more appropriate programming language 7
  8. Considering other languages • (enterprise) Java • python, ruby …

    • C/C++ ! • GOlang 8
  9. Get GOing 9

  10. The basics - package declaration package main import "fmt" func

    main() { fmt.Printf("Hello World!") } 10
  11. The basics - imports package main import "fmt" func main()

    { fmt.Printf("Hello World!") } 11
  12. The basics - main function package main import "fmt" func

    main() { fmt.Printf("Hello World!") } 12
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. Slices slice1 := make([]string, 3) // ["" "" ""] slice2

    := []string{"a", "b", "c"} // ["a" "b" "c"] slice1[0] = "a" // ["a" "" ""] var length = len(slice1) // 3 slice2 = append(slice2, "d") // ["a" "b" "c" "d"] 20
  21. Slices slice1 := make([]string, 3) // ["" "" ""] slice2

    := []string{"a", "b", "c"} // ["a" "b" "c"] slice1[0] = "a" // ["a" "" ""] var length = len(slice1) // 3 slice2 = append(slice2, "d") // ["a" "b" "c" "d"] 21
  22. Slices slice1 := make([]string, 3) // ["" "" ""] slice2

    := []string{"a", "b", "c"} // ["a" "b" "c"] slice1[0] = "a" // ["a" "" ""] var length = len(slice1) // 3 slice2 = append(slice2, "d") // ["a" "b" "c" "d"] 22
  23. Slices slice1 := make([]string, 3) // ["" "" ""] slice2

    := []string{"a", "b", "c"} // ["a" "b" "c"] slice1[0] = "a" // ["a" "" ""] var length = len(slice1) // 3 slice2 = append(slice2, "d") // ["a" "b" "c" "d"] 23
  24. Loops - classic while i := 0 for i <=

    3 { i = i + 1 } 24
  25. Loops - classic for for i := 0; i <=

    3; i++ { fmt.Println(i) } 25
  26. Loops - range numbers := []int{1, 2, 3} sum :=

    0 for i, val := range numbers { fmt.Println("index:", i) sum += val } fmt.Println("sum:", sum) 26
  27. Functions func sum(a int, b int) int { return a

    + b } // or func sum(a, b int) int { return a + b } 27
  28. Multiple return values func fancySum(a int, b int) (int, bool)

    { ok := true // some error checks return a + b, ok } func main() { sum, ok := fancySum(1, 2) if !ok { // fail with error } } 28
  29. Demo Time #1 A simple calculator 29

  30. Structs type person struct { firstname string lastname string age

    int } person1 := person{firstname: "John", lastname: "Doe", age: 28} person2 := person{"Jane", "Doe", 28} fmt.Println(person1.age) 30
  31. Interfaces type animal interface { color() string } 31

  32. Interfaces type cat struct { name string } type mouse

    struct { name string } 32
  33. Interfaces func (c cat) color() string { if c.name ==

    "Kitty" { return "black" } return "white" } func (m mouse) color() string { return "grey" } 33
  34. 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
  35. Goroutines • concurrent execution of code • synchronization needed for

    shared resources (mutex, lock) 35
  36. Goroutines func doSomeWork() { for i := 0; i <

    5; i++ { fmt.Println("Work index:", i) } } func main() { go doSomeWork() // continue with main thread flow } 36
  37. Channels • used for communication between goroutines • sender sends

    messages, receiver reads from channel • channels are basically blocking except they are buffered 37
  38. Channels // create a channel messages := make(chan string) //

    send a message messages <- "a message" // read a message msg := <- messages 38
  39. Channels // create a channel messages := make(chan string) //

    send a message messages <- "a message" // read a message msg := <- messages 39
  40. Channels // create a channel messages := make(chan string) //

    send a message messages <- "a message" // read a message msg := <- messages 40
  41. Demo Time #2 Goroutines 41

  42. Testing in Go import "testing" ... func TestTestName(t *testing.T) {

    // test code } 42
  43. 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
  44. Demo Time #3 Testing the calculator 44

  45. Tooling for Go • code formating • linting • testing

    • benchmarking • documentation • profiling 45
  46. my golang favorites • strong type system • error handling/multiple

    return values • implicit interfaces • built-in tooling • concurrency • speed 46
  47. 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
  48. Thanks for listening @fetzi_io 48