DEV Community

Pallat Anchaleechamaikorn
Pallat Anchaleechamaikorn

Posted on

1

จัดการ Go dependencies tools ด้วย Go mod

พอดีว่าไปเจอ repo หนึ่ง https://github.com/golang-templates/seed เขามีวิธีจัดการ tools ที่เขาใช้ในการ dev Go ผ่าน Go Module ซึ่งดูแล้วน่าสนใจเลยเอามาเล่าให้ฟัง

จาก repo นี้เขาใช้วิธีสร้าง go module ซ้อนไว้ใน project อีกชั้นหนึ่งแบบนี้

./
├── go.mod
├── go.sum
├── main.go
├── tools
│   ├── go.mod
│   ├── go.sum
│   └── tools.go

Enter fullscreen mode Exit fullscreen mode

แล้วเขียน Makefile กำกับไว้แบนี้

.PHONY: inst
inst: ## go install tools
    cd tools && go install $(shell cd tools && go list -e -f '{{ join .Imports " " }}' -tags=tools)
Enter fullscreen mode Exit fullscreen mode

เขาเขียนไว้ว่า ถ้ารันคำสั่ง make inst มันจะ cd เข้าไปใน tools แล้วสั่ง go install ของที่อยู่ใน import ลงมา
ซึ่งมันเวิร์คโดยเฉพาะกับ tools ต่างๆที่เขียนด้วย Go เหมือนกัน เช่น govulncheck หรือ golangci-lint เป็นต้น แต่ถ้าเป็นพวก tools ที่เขียนด้วยภาษาอื่น อันนี้ทำไม่ได้แน่นอน

จากที่ทดลอง มันติดอยู่อย่างหนึ่ง ถ้าเราเขียน repo แบบนี้ แล้วตั้งใจอยากจะทำเป็น template ให้คนอื่นมาสั่ง gonew ไปใช้ต่อ มันจะไม่สามารถเอา /tools ติดไปด้วยได้ แต่ก็เป็นแนวทางที่น่าสนใจดี สำหรับการนำไปใช้กับโปรเจคทั่วไปครับ

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs