Jonhnny Weslley / @jweslley
package main
import "fmt"
func main() {
quem := "Gurupi"
fmt.Printf("Fala galera do %s!\n", quem)
}
go run hello.go
int, uint, int8, uint8, ...
bool, string
float32, float64
complex64, complex128
[]int, [3]string, []struct{ Name string }
map[string]int
type Proxy struct {
httputil.ReverseProxy
servers Servers
tld string
}
*int, *Proxy
func AddrPort(addr string) (int, error)
chan bool
type Server interface {
Name() string
Port() int
}
// Interface do pacote `fmt`
type Stringer interface {
String() string
}
type server struct {
name string
port int
}
func (s *server) Name() string {
return s.name
}
func (s *server) Port() int {
return s.port
}
func (s *server) String() string {
return fmt.Sprintf("%s:%d", s.name, s.port)
}
s := server{"localhost", 3000}
fmt.Printf("Started at %s\n", s)
package io
type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
type ReadWriter interface {
Reader
Writer
}
package main
import (
"compress/gzip"
"encoding/base64"
"io"
"os"
"strings"
)
func main() {
var r io.Reader
r = strings.NewReader(data)
r = base64.NewDecoder(base64.StdEncoding, r)
r, _ = gzip.NewReader(r)
io.Copy(os.Stdout, r)
}
package main
import (
"log"
"net/http"
)
func Collector(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
monit := MonitFromXml(req.Body)
queue <- &monit
}
func main() {
http.HandleFunc("/collector", Collector)
log.Fatal(http.ListenAndServe(":8080", nil))
}
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
type dogpack struct {}
func (h *dogpack) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Processamento da requisição
}
func main() {
http.Handle("/", &dogpack{})
log.Fatal(http.ListenAndServe(":8080", nil))
}
type HandlerFunc func(ResponseWriter, *Request)
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
f(w, r)
}
HTTP error handlers
type errorHandler func(http.ResponseWriter, *http.Request) error
func handleError(f errorHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := f(w, r)
if err != nil {
log.Printf("%v", err)
http.Error(w, "Oops!", http.StatusInternalServerError)
}
}
}
HTTP error handlers
type errorHandler func(http.ResponseWriter, *http.Request) error
func handleError(f errorHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := f(w, r)
if err != nil {
log.Printf("%v", err)
http.Error(w, "Oops!", http.StatusInternalServerError)
}
}
}
func handler(w http.ResponseWriter, r *http.Request) error {
name := r.FormValue("name")
if name == "" {
return fmt.Errorf("empty name")
}
fmt.Fprintln(w, "Hi,", name)
return nil
}
http.HandleFunc("/form", handleError(handler))
Don't communicate by sharing memory. Instead, share memory by communicating.
i := pivot(s)
go sort(s[:i])
go sort(s[i:])
<-
é utilizado para enviar e receber valoresfunc compute(ch chan int) {
ch <- someComputation()
}
func main() {
ch := make(chan int)
go compute(ch)
result := <-ch
}
done := make(chan bool)
doSort := func(s []int) {
sort(s)
done <- true
}
i := pivot(s)
go doSort(s[:i])
go doSort(s[i:])
<-done
<-done
type Work struct { x, y, z int }
func worker(in <-chan *Work, out chan <- *Work) {
for w := range in {
w.z = w.x * w.y
out <- w
}
}
func Run() {
in, out := make(chan *Work), make(chan *Work)
for i := 0; i < 10; i++ {
go worker(in, out)
}
go sendLotsOfWork(in)
receiveLotsOfResults(out)
}
Compila e executa
go run hello.go
Executa os testes
go test
Gera binário e formata código
go build
go fmt
Baixa e instala dependências
go get github.com/jweslley/procker
Organizado automaticamente pelas ferramentas de build
workspace/
bin # executable binaries
pkg # compiled object files
src # source code
github.com/jweslley/
procker/
import "github.com/jweslley/procker"
Crie o diretorio em local de preferência
mkdir -p $HOME/gocode/src
Exporte a variavel GOPATH
export GOPATH=$HOME/gocode
Adicione o GOPATH/bin
ao PATH
export PATH=$PATH:$GOPATH/bin
Escolha um namespace
github.com/jweslley
no meu caso
Crie um diretório e programe alguma coisa
mkdir $GOPATH/src/github.com/jweslley/hello
cp hello.go $GOPATH/src/github.com/jweslley/hello
go install github.com/jweslley/hello
$GOPATH/bin/hello