Understanding Go Workspace and Package Structure
Workspace and Package Management in Go
The GOPATH Workspace
GOPATH is the working directory for Go development. It holds your source code and compiled executables. While traditionally all work occurred within GOPATH, the introduction of the go mod dependency management tool in Go 1.11 allows development outside this directory.
Setting Up GOPATH: Create a directory such as C:\go on Windows or ~/go on Linux. The standard GOPATH subdirectories are:
bin: Contains compiled executable binaries.src: Stores Go source code, organized into packages (each directory is a package).pkg: Stores compiled package objects (.afiles).
Configure environment variables as follows:
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
Note: GOPATH can list multiple directory paths, but these must differ from the Go installation directory.
Package Declaration and Import
The first statement in any Go source file must be a package declaration.
package greetings // 'greetings' is the package name
Package names are typically single, lowercase words (e.g., fmt). To avoid conflicts with the standard library, user-defined packages must use a unique base import path, such as a version control repository location (e.g., github.com/yourusername/). The package name declared in the file (package pkgname) should match the name of the immediate containing directory.
Crucial Rule: The package containing the main function, which defines an executable program, must be named main.
package main
Practical Example: Creating and Using a Package
1. Create a Custom Library Package
First, establish a new package directory. The method differs based on your dependency management approach.
# For GOPATH-based projects (pre-Go 1.11 or without modules):
mkdir -p $GOPATH/src/github.com/youruser/greeter
cd $GOPATH/src/github.com/youruser/greeter
# For Module-based projects (Go 1.11+):
mkdir -p ~/projects/greeter
cd ~/projects/greeter
go mod init github.com/youruser/greeter
Within the greeter directory, create a file named greeter.go.
package greeter // Package name matches the directory name
import "fmt"
// Greet prints a welcome message.
func Greet() {
fmt.Println("Welcome to the program.")
}
You can compile this library package:
go build github.com/youruser/greeter
2. Create an Executable Program Using the Library
Create a new directory for your application.
mkdir $GOPATH/src/github.com/youruser/apprunner
cd $GOPATH/src/github.com/youruser/apprunner
Create the main application file, app.go.
package main // Executables require the 'main' package
import (
"fmt"
"github.com/youruser/greeter" // Import the custom package
)
func main() {
fmt.Println("Starting application...")
greeter.Greet() // Call the function from the imported package
}
3. Install and Run the Executable
Use go install to compile the appplication and place the binary in $GOPATH/bin.
go install github.com/youruser/apprunner
This command creates an executable named apprunner in $GOPATH/bin. You can run it directly if $GOPATH/bin is in your system PATH.
apprunner
# Output:
# Starting application...
# Welcome to the program.