🐹 Go cheat sheet
Komutlar
go mod init modul/adi | Modül başlat |
go run . | Çalıştır |
go build -o arac . | Derle |
GOOS=linux GOARCH=amd64 go build -o arac-linux . | Çapraz derleme |
go test ./... | Testler |
go test -race ./... | Veri yarışı testi |
go vet ./... | Statik kontrol |
go fmt ./... | Biçimlendir |
go mod tidy | Bağımlılık düzenle |
Kalıplar
if err != nil { return err } | Hata kontrolü |
defer r.Body.Close() | Kapanışı garanti et |
c := http.Client{Timeout: 5 * time.Second} | Zaman aşımlı istemci |
var wg sync.WaitGroup; wg.Add(1); go func(){ defer wg.Done() }(); wg.Wait() | Goroutine bekleme |
ctx, cancel := context.WithTimeout(ctx, 3*time.Second) | Zaman aşımı bağlamı |
-ldflags "-X main.surum=1.2.3" | Sürümü derlemeye göm |
Şema
Go aracının yolu
- Kod (.go)
- go test
- go build (tek ikili)
- Distroless imaj / goreleaser
- Her yerde çalışır