Modules

27 articles
Circular Import in Go: How to Fix "import cycle not allowed" Resolve Go import cycles by extracting shared code into a new, independent package that both conflicting packages can import. Fix: "go.mod file not found" in Go The error occurs because your current directory is not the root of a Go module, meaning `go.mod` is missing or you are running commands in a subdirectory. Fix: "module not found" in Go Fix 'module not found' in Go by running go mod tidy to sync dependencies and update go.mod. Go Code Organization: When to Split Into Multiple Packages Split Go code into packages when files grow large, logic diverges, or you need to hide internal implementation details. How Go Packages Work: A Complete Guide Go packages group related source files under a single name to organize code and enable sharing across modules. How the Go Module Proxy Works (GOPROXY) GOPROXY is the environment variable that configures the list of servers Go uses to download module dependencies. How to Create and Publish a Go Module Initialize a go.mod file, tag a version in git, and push to a public repository to publish a Go module. How to Create Your Own Package in Go Initialize a Go module with go mod init and write your code in a .go file to create a package. How to Handle Diamond Dependency Problems in Go Go resolves diamond dependencies automatically by selecting a single version of shared packages, which can be explicitly controlled using go get. How to Import Packages in Go Import Go packages using the import statement with the package path or module URL. How to Organize a Go Project: Standard Project Layout Use cmd for apps, internal for private code, and pkg for public libraries in your Go project. How to Organize Code in a Go Project Organize Go projects by creating a module with a go.mod file and separating executables in cmd from libraries in subdirectories. How to Set Up a Private Go Module Proxy Configure the GOPROXY environment variable to point to your private Go module proxy URL. How to Structure a Large Go Application Organize large Go apps using cmd for entry points, pkg for public libraries, and internal for private code. How to Use a Private Git Repository as a Go Module Set the GOPRIVATE environment variable to your repository domain to bypass the public proxy and fetch private Go modules directly from Git. How to Use Build Constraints and Build Tags in Go Use //go:build comments and the -tags flag to conditionally compile Go code for specific environments. How to Use Internal Packages in Go Import internal packages in Go by adding their full module-relative path to the import block. How to Use replace in go.mod for Local Development Use the replace directive in go.mod to point a module path to a local directory for development testing. How to Use Workspaces in Go (go.work) Initialize a Go workspace with go work init and add modules using go work use to manage multiple projects together. How to Vendor Dependencies in Go with go mod vendor Use `go mod vendor` to copy all your module dependencies into a `vendor` directory within your project, ensuring your build is isolated from external network issues and specific versions are locked locally. How to Version a Go Module with Semantic Versioning Retract a Go module version by adding a retract directive to go.mod, bumping the version, and pushing a new tag. Monorepo vs Multi-Repo for Go Projects Choose monorepo for shared code and atomic updates, or multi-repo for independent deployment and access control in Go projects. What Is the Difference Between a Package and a Module in Go A module is a versioned project unit defined by go.mod, while a package is a single directory of compiled Go code. What Is the go.sum File and Why You Should Commit It The go.sum file stores dependency checksums to ensure build reproducibility and security, so you must commit it to your repository. What Is the init Function and Package Initialization Order The `init` function is a special function in Go that runs automatically before `main()` to set up package state, and it executes in a specific order: imports first (depth-first), then the package's own `init` functions. What Is the v2+ Import Path Problem in Go Modules Fix the v2+ import path error by adding the major version suffix (e.g., /v2) to the module path in go.mod and all import statements. When to Use Packages vs Interfaces for Modularity in Go Use interfaces for defining behavior contracts between modules and packages for grouping related implementation code.