Ang Go kay may powerful set sa command-line tools para mag-build, mag-format, mag-analyze, ug mag-manage ug code. Dili pareha sa uban na kailangan pa ug third-party tools — Go naay built-in na. Anhi nga chapter: essential tools na ginagamit sa Go developers adlaw-adlaw.
go build nag-compile ug Go source files into executable binaries. Kung unsa lang kelangan i-compile, ug i-cache ang results para paspas. Wa nay compilation nga magsige2 ug re-build sa wala nag-usab:
go build # build current package
go build -o myapp main.go # specify output name
go build ./... # build all packages in module
// Ang binary ibutang sa current directory (or $GOPATH/bin with go install)
Goal: Paghimo ug simple Go program into executable binary. I-build dayon, ayawg sige ug go run sa production.
// Create main.go:
package main
import "fmt"
func main() {
fmt.Println("Built with go build!")
}
// Terminal:
// go build -o myprogram
// ./myprogram (or myprogram.exe on Windows)
go run nag-compile ug run Go code sa isa ka step — walay binary na nabilin. Mapuslanon para quick experiments ug scripts. Kung gusto ka lang mag-test2 ug code, go run imong amigo:
go run main.go
go run . # run the current package
go run main.go utils.go # run multiple files
// go run is slower than running the built binary (compilation every time)
Goal: Magsulat ug program ug i-run diretso na walay binary. Compile + run in one. De-stress sa imong life.
// Create hello.go:
package main
import "fmt"
func main() {
fmt.Println("Running with go run!")
}
// Terminal:
// go run hello.go
gofmt awtomatikong nag-format ug Go code according sa standard style. Nag-enforce ug consistent indentation (tabs — dili spaces ha!), spacing, brace placement, ug line breaks. Kung mag-away mo sa imong ka-team tungod sa formatting, gofmt ang mag- Judge Judy:
gofmt -l . # list files that need formatting
gofmt -w main.go # format in-place
gofmt -d main.go # show diff of changes
// Most editors run gofmt on save automatically.
Goal: Magsulat ug poorly formatted Go code ug gamit ug gofmt para i-fix. Magsugod tag gubot, matapos tag limpyo.
// messy.go:
package main
import "fmt"
func main() {
x:=42
if x>0{
fmt.Println("positive")}}
// Terminal:
// gofmt -w messy.go
// gofmt reformats to standard style
goimports parehas sa gofmt pero nag-manage pud ug import statements — nag-add ug missing ug nag-remove ug unused. Import sa fmt pero wa nagamit? Tangtangon. Gamitag Println pero wa kay fmt? I-add dayon:
go install golang.org/x/tools/cmd/goimports@latest
goimports -w main.go # format and fix imports
// Awtomatikong mu-add ug "fmt" kung nagamit ug fmt.Println
// Mu-remove ug "os" kung wala nagamit ug os functions
Goal: Magsulat ug code na may missing imports ug pahims-a ang goimports mu-fix. Magpaka-tamad ta — ang tool na bahala:
// missing.go:
package main
func main() {
fmt.Println("Hello")
log.Println("Logging")
}
// Terminal:
// goimports -w missing.go
// Adds "fmt" and "log" to the import block
go vet nag-report ug suspicious constructs — dili errors, pero code na murag wrong. Murag imung amigo nga mag-ingon "bai, sure ka anang imong gi-type?":
go vet main.go # vet a single file
go vet ./... # vet all packages
go vet -v ./... # verbose output
// Ma-detect: unreachable code, wrong printf arguments, suspicious assignments,
// lock copying, deferred Close check, etc.
Goal: Magsulat ug code na may common vet warning ug run go vet para ma-detect. I-test nato kung ga-duty ba si go vet.
// vetexample.go:
package main
import "fmt"
func main() {
x := 10
fmt.Printf("x = %d", x, "extra arg") // wrong number of args
}
// Terminal:
// go vet vetexample.go
// Output: Printf call has arguments but no formatting directives
Ang race detector nag-detect ug concurrent access bugs. I-enable gamit ang -race flag. Kung naa kay goroutines, -race gyud dapat — ayawg padalos2:
go run -race main.go # run with race detection
go build -race -o app . # build with race detection
go test -race ./... # test with race detection
// Nag-report sa tanang data races with stack traces.
// Mas hinay ang binary with -race, gamiton lang para testing, dili production.
Goal: Magsulat ug program na may race condition ug gamit ang race detector para makit-an. Ipakita nato kung unsa hitsura ang race condition in action:
package main
import (
"fmt"
"sync"
)
func main() {
var counter int
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
counter++ // race!
}()
}
wg.Wait()
fmt.Println(counter)
}
// go run -race main.go
// Output shows the race warning
go doc nag-display ug documentation para sa bisan unsa na package, function, or type diretso sa terminal. Dili naka mag-open ug browser para lang magbasa ug docs — tamad kaayo ka ana:
go doc fmt # package docs
go doc fmt.Println # function docs
go doc time.Time # type docs
go doc time.Timer.Reset # method docs
go doc -src fmt.Println # show source code
Goal: Gamit ug go doc para maka-learn about sa strings package. Terminal ra imong need, ayawg search sa Google:
// Terminal:
// go doc strings
// go doc strings.Contains
// go doc strings.Builder
go get nag-download ug nag-install ug packages ug modules. Sa Go 1.17+, gamit ug go install para executables ug go get para mag-update ug go.mod dependencies. Kung gusto kag external package, go get imong buddy:
go get github.com/gin-gonic/gin # add/update dependency
go get -u ./... # update all direct dependencies
go mod tidy # clean up go.mod and go.sum
// Install a tool binary:
go install golang.org/x/tools/cmd/goimports@latest
Goal: I-add ang external dependency (github.com/google/uuid) ug gamiton para mag-generate ug UUID. Importante ni — kahibaw naka mag-generate ug unique IDs:
// Terminal:
// go get github.com/google/uuid
// main.go:
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
id := uuid.New()
fmt.Println("UUID:", id)
}
// go mod tidy (cleans up go.sum)
// go run main.go
Goal: Kuha ug messy Go file, i-run ang tools in sequence para ma-fix, ug i-add ang external dependency. Murag dalan sa pagka-Go developer — magsugod sa gubot, matapos sa limpyo:
// Start with this messy file:
package main
import "fmt"
import "os"
func main() {
x:=10
if x>0{
fmt.Println("positive")
}
os.Getenv("HOME")
}
// Run these in order:
// goimports -w main.go (fixes imports and formatting)
// go vet main.go (check for issues)
// go get github.com/google/uuid
// go mod tidy
// Ang result kay clean, properly imported, ug vetted Go code.
go build — compile to binary. Pang production.go run — compile and run in one step. Pang testing lang.gofmt / goimports — automatic formatting and import management. Dili na maglalis ug formatting.go vet — static analysis para suspicious code. Imong spidey sense.go run -race — detect data races. Gamitun jud ni.go doc — read documentation sa terminal. No more browser tabs.go get / go mod tidy — dependency management. Kung wa kay external package, ayawg re-invent the wheel.