$30 off During Our Annual Pro Sale. View Details »

Go, goroutines e o gopher

Go, goroutines e o gopher

Conheça um pouco da história do Go e sua estrutura.

Alexandre Vicenzi

October 10, 2016
Tweet

More Decks by Alexandre Vicenzi

Other Decks in Programming

Transcript

  1. Go, Goroutines e o Gopher
    Alexandre Vicenzi

    View Slide

  2. O que é Go?
    Go é uma linguagem de programação desenvolvida pela Google para
    ajudar a resolver problemas do Google.

    View Slide

  3. Origem
    ● 2007 - 2009
    ● Google
    ● Robert Griesemer, Rob Pike e Ken Thompson

    View Slide

  4. Proposta
    ● Concorrente
    ● Compilar rapidamente
    ● Garbage Collector
    ● Estaticamente tipada

    View Slide

  5. Quem está usando?

    View Slide

  6. Hello World
    import “fmt”
    func main() {
    fmt.Println(“Olá mundo!”)
    }

    View Slide

  7. Diferenciais
    ● Orientação a Objetos sem herança
    ● Sem exceções
    ● Imports e variáveis não usadas causam erro de compilação
    ● Sem method overloading ou parâmetros opcionais
    ● Visibilidade é definida pela primeira letra
    ● Interfaces sem declaração (duck typing)

    View Slide

  8. Sem exceções
    import (
    "os"
    "log"
    )
    file, err := os.Open("file.txt")
    if err != nil {
    log.Fatal(err)
    }

    View Slide

  9. Duck typing
    Em linguagens dinâmicas como Python existem o conceito de Duck Typing.
    "if it looks like a duck and quacks like a duck, it’s a duck."
    class Duck:
    def quack(self):
    print("quack")
    class Cow:
    def quack(self):
    print("muu")
    def say_quack(quacker)
    quacker.quack()
    say_quack(Duck())
    say_quack(Cow())

    View Slide

  10. Duck Typing em Go
    type Duck struct {
    }
    func (d *Duck) Quack() {
    fmt.Println("Quack")
    }
    func SayQuack(q Quacker) {
    q.Quack()
    }
    SayQuack(&Duck{})
    SayQuack(&Cow{})
    type Cow struct {
    }
    func (c *Cow) Quack() {
    fmt.Println("Muu")
    }
    type Quacker interface {
    func Quack()
    }

    View Slide

  11. Concorrência
    ● Goroutines
    ● Channels

    View Slide

  12. Goroutines
    ● Função que executa concorrentemente
    ● São basicamente threads
    ● Basta adicionar a keyword go na chamada do método
    ● Muito similar ao comando & do shell, executa e termina silenciosamente

    View Slide

  13. Goroutines
    func f(from string) {
    for i := 0; i < 3; i++ {
    fmt.Println(from, ":", i)
    }
    }
    func main() {
    f("direct")
    go f("goroutine")
    go func(msg string) {
    fmt.Println(msg)
    }("going")
    var input string
    fmt.Scanln(&input)
    fmt.Println("done")
    }
    $ go run goroutines.go
    direct : 0
    direct : 1
    direct : 2
    goroutine : 0
    going
    goroutine : 1
    goroutine : 2

    done

    View Slide

  14. Channels
    ● São pipes que conectam goroutines
    ● Simples para trocar mensagens entre goroutines
    ● Unbuffered e síncrono por padrão
    ● O sender fica bloqueado até o receiver receber o valor
    ● Buffered channels bloqueiam apenas se o buffer ficar cheio

    View Slide

  15. Channels
    package main
    import "fmt"
    func main() {
    messages := make(chan string)
    go func() { messages <- "ping" }()
    msg := <-messages
    fmt.Println(msg)
    }
    $ go run channels.go
    ping

    View Slide

  16. E mais
    ● Ponteiros
    ● Pacotes
    ● Defer, Panic e Recover
    ● Testes
    ● Controle de dependências
    ● C? Go? Cgo!

    View Slide

  17. E o Gopher?
    Você já conheceu ele :)
    ● Desenhado por Renée French
    ● Inspirado no gopher de uma promoçao de camisa da rádio WFMU

    View Slide

  18. Dúvidas?

    View Slide

  19. Referências
    ● The Go Programming Language
    ● Go by Example
    ● An Introduction to Programming in Go

    View Slide

  20. Obrigado
    @alxvicenzi
    alexandrevicenzi.com
    github.com/alexandrevicenzi

    View Slide