O documento apresenta uma introdução à linguagem de programação Go. Ele discute a história e criadores da linguagem, suas principais características como tipagem estática e concorrência, e fornece exemplos de código como "Hello World" e variáveis. O documento também lista recursos online para iniciar com a linguagem Go.
2. História
• 2007 - Design
• Robert Griesemer (HotSpot/V8)
• Rob Pike (Unix/UTF-8)
• Ken Thompson (Unix/UTF-8/B e C)
• 2009 - Open Source
• http://golang.org/doc/faq#history
3. Características
• Compilada
• Tipagem estática
• Sintaxe simples
• Visibilidade pela primeira letra do símbolo
• Concorrência (Goroutines)
• Garbage collector
• Ótima biblioteca padrão
6. Por onde iniciar?
• http://golang.org/doc/install
• http://tour.golang.org/
• http://play.golang.org/
• http://www.golang-book.com/
• http://www.golangbootcamp.com/
7. Hello World
// Definição do pacote
package main
// Import do pacote fmt
import "fmt"
// Definição da função main
func main() {
// Executa a função Println do pacote fmt
fmt.Println("Hello World")
}
34. Range
package main
import "fmt"
func main() {
alunos := make([]string, 3)
alunos[0] = "Fulano"
alunos[1] = "Beltrano"
alunos[2] = "Cicrano"
for i, value := range alunos {
fmt.Println("Posição:", i, "Valor:", value)
}
}
37. Maps
package main
import "fmt"
func main() {
m := make(map[string]int)
m["Teste"] = 30
fmt.Println("O valor é:", m["Teste"])
m["Teste"] = 31
fmt.Println("O valor é:", m["Teste"])
delete(m, "Teste")
fmt.Println("O valor é:", m["Teste"])
_, ok := m["Teste"]
if ok == false {
fmt.Println("O valor Teste não existe")
}
}
38. Maps
package main
import "fmt"
func main() {
m := make(map[string]int)
m["Allisson"] = 32
m["Chuck Norris"] = 75
for k, v := range m {
fmt.Println("Chave:", k, "Valor:", v)
}
}
40. Goroutines
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
func fetch(s string) {
res, err := http.Get(s)
if err != nil {
log.Println("Não foi possível conectar com a url", s)
return
}
body, err := ioutil.ReadAll(res.Body)
fmt.Println("A página:", s, "Tem o tamanho de:", len(string(body)))
}
func main() {
go fetch("http://www.google.com.br")
fetch("http://www.uol.com.br")
time.Sleep(5000 * time.Millisecond)
}
41. Goroutinespackage main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func fetch(s string, channel chan string) {
res, err := http.Get(s)
if err != nil {
log.Println("Não foi possível conectar com a url", s)
return
}
body, err := ioutil.ReadAll(res.Body)
result := fmt.Sprintf("A página: %s, Tem o tamanho de %d", s, len(string(body)))
channel <- result
}
func main() {
channel := make(chan string)
go fetch("http://linuxmint.com/", channel)
go fetch("http://www.ubuntu.com/", channel)
go fetch("http://www.debian.org/", channel)
go fetch("http://www.opensuse.org/en/", channel)
go fetch("http://getfedora.org/", channel)
for i := 0; i < 5; i++ {
fmt.Println(<-channel)
}
}
42. HTTP Server
package main
import (
"io"
"net/http"
)
func index(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world!")
}
func main() {
http.HandleFunc("/", index)
http.ListenAndServe(":8080", nil)
}