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

GO Meetup

GO Meetup

Evento que ocorreu em Lisboa no dia 12 de dezembro de 2016

Renato Suero

December 16, 2016
Tweet

More Decks by Renato Suero

Other Decks in Programming

Transcript

  1. Um pouco de história Em 2007 começa um projeto interno

    no google. Desenvolvido por Rob Pike, Ken Thompson e Robert Griesemer. Em novembro de 2009 o google abriu o fonte da linguagem
  2. mkdir -p ~/go/{bin,src,pkg} src Go source code organized into packages

    pkg OS and architecture specific compilation artifacts bin Executable Go programs Workspace
  3. https://golang.org/cmd/go/ go run arquivo.go executa seu codigo go build gera

    um arquivo executável go test executa a suíte de testes go env mostrará variáveis de ambiente go get baixa e instala pacotes e dep. Go Tools
  4. package main func init import . "fmt" import f "fmt"

    Import _ "fmt" Packages func privada(){ } func Publica(){ }
  5. // Comentário para uma única linha /* */ Comentário para

    múltiplas linhas Comentários func main() { //Comentário de uma linha fmt.Println("GO Meetup") /* Comentarios para múltiplas linhas */ }
  6. const name *type(opcional) = value Constantes package main import "fmt"

    const country string = "Brasil" const name = "Renato Suero" func main() { fmt.Println(name, country) }
  7. var name type Variáveis package main import "fmt" var name

    = "Renato" var age int func main() { age = 30 country := "Brasil" fmt.Println(name, age,country) }
  8. if / else if / else swtich case Condicional func

    main() { lang := "GO" if lang == "GO"{ fmt.Println("=)") }else { fmt.Println("=(") } } func main() { lang := "GO" switch lang { case "GO": fmt.Println("=)") default: fmt.Println("=(") } }
  9. for condition , em GO não existe while Repetição package

    main import "fmt" func main() { for i:= 0; i < 5; i++ { fmt.Println(i) } } package main import "fmt" func main() { i:= 0 for i < 5 { fmt.Println(i) i++ } }
  10. Repetição - Range package main import "fmt" func main() {

    cities := [3]string{"Lisboa","Sintra","Porto"} for index,value := range cities{ fmt.Println("Index ",index," Value ",value) } }
  11. var name [size]type Array package main import "fmt" func main()

    { x := [2]int{1, 2} y := [...]int{3, 4,5} fmt.Println(x) fmt.Println(y) }
  12. var name []type Slice package main import "fmt" func main()

    { x := [2]int{1, 2} y := []int{3, 4, 5} y = append(y, 666) fmt.Println(x) fmt.Println(y) }
  13. var name map[type]type Maps package main import "fmt" func main()

    { config := map[string]string{ "port" : "22", "server" : "127.0.0.1"} fmt.Println(config) fmt.Println(config["port"]) }
  14. func name (name type) Funcs package main import "fmt" func

    main() { ShowText() } func ShowText(){ fmt.Println("Hi") } package main import "fmt" func main() { ShowText("Renato") } func ShowText(name string){ fmt.Println("Hi",name) }
  15. func name (params) type(return) Funcs - Return package main import

    "fmt" func main() { fmt.Println(ShowText()) } func ShowText()string{ return "Hi GO" } package main import "fmt" func main() { fmt.Println(ShowText()) } func ShowText() (m string) { m ="Hi GO" return }
  16. Funcs - Multiple Return package main import "fmt" func main()

    { n,m := ShowText() fmt.Println(n,m) } func ShowText() (x,y int) { x = 420 y = 666 return }
  17. Funcs - Variadics package main import "fmt" func main() {

    Multiple(1,2,3,4) } func Multiple(args...int) { for _,v := range args{ fmt.Println(v*2) } } _ underline,usado para ignorar um valor. * em package apenas para carregar a func init
  18. Ponteiros package main import "fmt" func FuncValue(x int){ x =

    666 return x } func FuncPointer(x *int){ *x = 420 } // func man ---> func main() { n := 10 fmt.Println(n) fmt.Println(FuncValue(n)) FuncPointer(&n) fmt.Println(n) }
  19. type name struct {vars} Structs package main Import "fmt" type

    User struct{ name string age int } func main() { user := User{name: "Renato",age: 30} fmt.Printf("%+v \n",user)//user.name ="Renato Suero" }
  20. Func (receiver) name…. Métodos package main import "fmt" type User

    struct{ name string age int } func (u User) SayHello(){ fmt.Println("Hi",u.name) }// func man ---> func main() { user := User{name:"Renato"} user.age = 30 user.SayHello() }
  21. type name interface { methods} Interface package main import "fmt"

    type Operation interface{ Calculate()int } type Sum struct{ x,y int} type Sub struct{ x,y int}
  22. Interface func (s Sum) Calculate() int{ return s.x + s.y

    } func (s Sub) Calculate() int {return s.x - s.y } func main() { sum := Sum{10,20} sub := Sub{10,5} operations := []Operation{sum,sub} for _,v := range operations { fmt.Println("Resultado: ", v.Calculate()) } }
  23. Goroutines package main import "fmt" Import "time" func showText(n int)

    { for i := 0; i < 3; i++ { fmt.Println(n) time.Sleep(200 * time.Millisecond) } } func main() { go showText(2) showText(3) } //Sem a goroutine o resultado será // 2 2 2 3 3 3 //Com a goroutine o resultado será // 3 2 2 3 3 2
  24. name := make(chan type,*buffeSize) Channels package main import "fmt" func

    main() { c := make(chan int) go setValue(c) valor := <- c fmt.Println(valor) } func setValue(c chan int){ c <- 666 }
  25. Testes Arquivos terminados com *_test.go, serão executados pela suíte de

    testes, mas ignorados no buid; É necessário importar o pacote testing; As funções começam com Test* serão executadas,assim podemos criar auxiliares/setups; Deve receber um ponteiro de testing.T como parametro;
  26. Testes - exemplo package main import "testing" func TestSoma(t *testing.T)

    { n := Soma(600, 66) if n != 666 { t.Error("Resultado diferente do esperado") } } func Soma(x, y int) int { return x + y }