Setting Up a New Go Project and 5 Essential Go Commands
When I started with Golang, I quickly realized how important it is to set up projects correctly and master some everyday commands. In this post, I’ll share my process for setting up a new Go project and the commands I use daily while writing Go, plus a few debugging tips to make your life easier.
Step 1: Create a New Project Directory
Start by creating a folder for your project:
mkdir my-go-project
cd my-go-project
- Initialize a Go Module
Use go mod init to initialize a Go module. This creates a go.mod file to manage dependencies:
go mod init my-go-project
The module name (e.g., my-go-project) should ideally match the repository name if you plan to use version control.
Step 2: Create Your Entry File
Create a file named main.go
and add a basic main()
function:
package main
import "fmt"
func main() {
fmt.Println("Hello, Go World!")
}
Step 3: Run Your Project
Run your project with:
go run main.go
You should see:
Hello, Go World!
5 Essential Go Commands Every Developer Must Know
Here are some essential Go commands you’ll find yourself using daily:
Run Your Code
Run your code without building an executable:
go run main.go
- Build an Executable
Compile your code into an executable file:
go build
Building an executable creates a standalone binary file that can be run directly without needing Go installed. This is especially useful for:
Deployment: Easily distribute the binary to servers or users.
Offline Usage: Run the program without requiring an internet connection.
End-User Convenience: Users can execute the program without dealing with source code or dependencies.
This creates a binary named after your project folder (e.g., my-go-project). Run the executable with:
./my-go-project
- Format Your Code
Automatically format your Go code according to the official Go style guide:
go fmt ./...
- Run Tests
Run tests in your project:
go test ./...
- Manage Dependencies
Download and tidy dependencies:
go mod tidy
This ensures your go.mod
and go.sum
files are up to date.
Debugging Tips for Go Developers
Debugging in Go is straightforward when you use the right tools and techniques. Here are my top tips:
- Use log for Debugging Output
Replace fmt.Println with log.Println for better debugging. The log package includes timestamps and is easier to track:
import "log"
func main() {
log.Println("Debugging message: Hello, Go World!")
}
- Use a Debugger Like dlv (Delve)
Delve is a powerful debugger for Go:
Install Delve:
go install github.com/go-delve/delve/cmd/dlv@latest
Start debugging your program:
dlv debug main.go
- Set Breakpoints in VS Code
If you’re using VS Code, install the Go extension and configure your debugger. Set breakpoints by clicking next to the line numbers and run the debugger.
- Inspect Variables
Use log.Printf to inspect variable values:
log.Printf("Value of x: %v", x)
- Handle Panics Gracefully
Recover from panics in your program by using the recover function:
func main() {
defer func() {
if r := recover(); r != nil {
log.Printf("Recovered from panic: %v", r)
}
}()
panic("Something went wrong!")
}
Conclusion
Setting up a new Go project and mastering essential commands can streamline your development process. Add some debugging techniques to your toolkit, and you’re ready to tackle any challenges Go throws your way.
What are your favorite Go commands or debugging tips? Share them in the comments or tag me on LinkedIn — I’d love to learn from your experience too! 😊
"When sharing, don't forget to tag me, Florence Okosun!"