Skip to main content

Basics


Installation​

wget https://go.dev/dl/go1.17.5.linux-amd64.tar.gz
sudo mkdir -p /opt/go/1.17.5
pv -ptrb go1.17.5.linux-amd64.tar.gz | sudo tar -xzp -C /opt/go/1.17.5
export PATH=/opt/go/1.17.5/go/bin:$PATH

Setup​

mkdir <package>
cd <package>
go mod init github.com/<user>/<package>
go mod tidy

λͺ¨λ“ˆμ˜ μ†ŒμŠ€μ½”λ“œμ™€ go.mod νŒŒμΌμ„ λΉ„κ΅ν•˜μ—¬ λͺ¨λ“ˆμ„ μΆ”κ°€ λ˜λŠ” μ œκ±°ν•©λ‹ˆλ‹€.

Build​

[envs] go build [flags] [-o <output>]
  • [envs]
    • GOOS=<os>
    • GOARCH=<arch>
    • CGO_ENABLED=0|1: cgo μ‚¬μš© μ—¬λΆ€
  • [flags]
    • -a: μ‚¬μš©λ˜λŠ” λͺ¨λ“  νŒ¨ν‚€μ§€λ₯Ό κ°•μ œλ‘œ λ‹€μ‹œ λΉŒλ“œν•©λ‹ˆλ‹€.
    • -ldflags 'flags'
      • flags
        • -s: static linking

Ex) CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -a -ldflags '-s' -o app

info

μ‚¬μš© κ°€λŠ₯ν•œ GOOS와 GOARCHλŠ” go tool dist list | column -c 75 | column -t λͺ…λ Ήμ–΄λ‘œ μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

go install

λΉŒλ“œλœ λ°”μ΄λ„ˆλ¦¬κ°€ GOBINκ²½λ‘œμ— μ„€μΉ˜λ©λ‹ˆλ‹€.

Reference​