Setting Up a Go Project with Standard Conventions
This guide creates a Go project following community standards for structure and naming. Requires Go installation (golang.org).
Initialise Project
Create and initialise a new Go project with a single bash script:
# Replace with your values
USERNAME="your-github-username"
PROJECT_NAME="project-name"
# Create project structure
mkdir -p ~/projects/$PROJECT_NAME
cd ~/projects/$PROJECT_NAME
go mod init github.com/$USERNAME/$PROJECT_NAME
# Alternative: Create project with one command
# mkdir -p ~/projects/$PROJECT_NAME && cd ~/projects/$PROJECT_NAME && go mod init github.com/$USERNAME/$PROJECT_NAME
Note: Project names should be lowercase with hyphens instead of spaces.
Folder Structure
The following is the folder structure that we will be using for our project.
<project-name>
├── README.md
├── cmd
│ └── main.go
├── go.mod
├── go.sum
└── internal
└── pkg
└── pkg.go
To create this folder structure, we will use the following commands, where pkg-name is the name of the package that we will be creating and command-name is the name of the command that we will be creating.
mkdir -p cmd/command-name
mkdir -p internal/pkg-name
touch README.md
touch cmd/command-name/main.go
touch internal/pkg-name/pkg.go
Folder Structure Explained
The folder structure follows the standard Go project layout, a widely adopted convention in the Go community. While not mandatory, it promotes consistency and best practices. For details, visit the Standard Go Project Layout.
cmd/
: Contains commands or executables for the project.internal/
: Holds internal packages used exclusively within the project.README.md
: Provides project documentation.go.mod
andgo.sum
: Manage project dependencies.