12/04/2026 18:15pm

EP.11 Go and Modules: Managing Dependency Professionally
#programming
#Go
#Go Modules
#Dependency Management
Go and Modules: Managing Dependency Professionally
No more headaches with manually installing packages! Today, we will guide you through using Go Modules, which will help keep your project organized and ready for full development.
1. What are Go Modules?
Go Modules allow you to manage packages and dependencies efficiently, eliminating the need to worry about manually installing library files.
2. Getting Started with Go Modules
When you create a new project, use this command to generate a go.mod file:
go mod init myapp
The go.mod file will store the main module of your project, along with the various dependencies you will need.
3. Installing External Packages with go get
Suppose you want to install a web framework called Gin. Use the command:
go get github.com/gin-gonic/gin
When you run this command, the package information will be added to go.mod, and the necessary files will be downloaded to your machine.
4. Updating and Managing Dependencies
Go has a clear version management system for modules, such as:
go get -u github.com/gin-gonic/gin
Locking the dependency versions you want in go.mod to reduce the risk of unintended updates.
5. Running a Project that Uses Go Modules
After successfully installing the modules, run your project with:
go run main.goExample:
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello, Gin!")
})
r.Run() // รันเซิร์ฟเวอร์บนพอร์ต 8080
}