Nâng CaoGolang iconGolang

gofmt vs goimports vs golangci-lint — khác nhau thế nào?

gofmt là formatter chính thức của Go, chuẩn hóa indentation (tab), spacing, và cú pháp. Không có option cấu hình — một chuẩn duy nhất cho toàn bộ cộng đồng Go. Chạy: gofmt -w .

goimports = gofmt + tự động thêm/xóa import statements. Đây là công cụ được khuyến nghị dùng trong editor (VS Code, GoLand) thay thế gofmt.

bash
goimports -w .          # format + fix imports
goimports -l .          # chỉ list files cần fix

golangci-lint là meta-linter chạy hàng chục linters song song: staticcheck (bugs tiềm ẩn), errcheck (bỏ sót check error), govet (misuse of sync primitives), revive (code style), gosec (security), v.v.

bash
golangci-lint run ./...
golangci-lint run --fix ./...  # tự fix một số issues

Cấu hình qua .golangci.yml:

yaml
linters:
  enable:
    - errcheck
    - staticcheck
    - gosimple
    - unused
    - revive

Best practice: tích hợp goimports vào editor (save hook), chạy golangci-lint trong CI pipeline và fail build nếu có lỗi.

Xem toàn bộ Golang cùng filter theo level & chủ đề con.

Mở danh sách Golang