Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go for Elephants

Go for Elephants

A brief introduction to Go for PHP programmers

Sergio Vera

December 16, 2015
Tweet

More Decks by Sergio Vera

Other Decks in Programming

Transcript

  1. At a glance PHP • Created in 1994 by Rasmus

    Lerdorf • Interpreted • Dynamically typed Go • Created in 2009 by Ken Thompson, Rob Pike and Robert Griesemer • Compiled • Statically typed
  2. We use the function Println from package fmt to print

    result on screen Programs must declare a function named main, which will be its entrypoint Functions are declared with the keyword func. Note the reversed order in input parameters type declaration Comments follow the usual C style. Every Go program is made up of packages. Programs start running in package main A first example package main import "fmt" // Returns the result of adding two integers func add(x int, y int) int { return x + y } func main() { fmt.Println(add(42, 13)) } package main import "fmt" // Returns the result of adding two integers func add(x int, y int) int { return x + y } func main() { fmt.Println(add(42, 13)) }
  3. Capitalized properties/functions are exported (accessible outside package) OOP Go is

    a procedural language but implements objects in a very particular way, using structs, receivers and composition. class Person { public $name; private $birthDate; public function age() { return $this->birthDate->diff( new DateTime(‘now’) )->y; } } Person is the receiver of Age() type Person struct { Name string birthDay time } func (p Person) Age() int { now := time.Now() years := now.Year() - p.birthDay.Year() if now.YearDay() < p.birthDay.YearDay(){ years-- } return years }
  4. Composition Instead of inheritance, Go uses a mechanism called composition,

    that’s kind of a puzzle approach. A struct can include multiple other structs, acting as multiple inheritance. class Cuban extends Person { ... } class Ivan extends Cuban { ... } type Ivan struct { Person Cuban ... }
  5. Interfaces Go uses implicit interfaces, that is, if a struct

    has all methods of an interface, this struct implements that interface. interface Communist { public function expropriate(); } class Ivan implements Communist { ... } type communist Interface { func expropriate() } type Ivan struct { ... } func (i Ivan) expropriate() { ... }
  6. Concurrency: Channels and Goroutines A goroutine is a lightweight thread

    of execution that will execute concurrently with the calling one. Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values into another goroutine.
  7. The <-messages syntax receives a value from the channel, "ping"

    in this case From a new goroutine, we send “ping” to the messages channel we created before Create a new channel. Channels are typed by the values they convey Concurrency: Go example func main() { messages := make(chan string) go func() { for { msg := "ping" messages <- msg fmt.Println(msg) time.Sleep(1 * time.Second) } }() for { select { case <-messages: fmt.Println("pong") } } }
  8. Tools Go provides a vast suite of tools. Among of

    them we have: • go fmt: The standard Go code style is dictated by this tool. The format this tool outputs is the accepted format of the source code. • go test: Go comes with a simple testing tool, including code coverage. • go doc: Generates documentation from source code and comments.
  9. To learn more... • A Tour of Go https://tour.golang.org/welcome/1 •

    Go by example https://gobyexample.com/ • An Introduction to Programming in Go https://www.golang-book.com/books/intro