1.1 Install Golang & Set Up Code Editor
TL;DR
- Install Go, the programming language itself.
- Install VS Code, a free and popular code editor.
- Add the Go extension to VS Code to give it Go superpowers.
- Enable "Format on Save" in VS Code settings.
- Now, your Go code will automatically get cleaned up every time you hit save. ✨
Why This Matters
Ever seen code that looks like a messy bedroom? Indentations are all over the place, spacing is random, and it's just hard to read. Cleaning it up by hand is tedious and a total waste of brainpower. You should be focused on solving problems, not on whether you used tabs or spaces.
This setup solves that forever. By automating your code formatting, every Go file you write will be perfectly clean and consistent. It's a tiny change that makes your life as a developer way easier and helps you collaborate with others without arguing about style.
Concepts in Plain English
Before we dive in, let's get a few key ideas straight. Don't worry, there's no quiz!
- Go: The actual programming language, like the English language with its own grammar and vocabulary.
- VS Code: Your workshop or text editor. It's where you write your code, but it doesn't know Go's grammar rules by default.
- Go Extension: A special toolbox for your workshop that teaches VS Code how to understand Go.
gopls: Your expert assistant that lives in the toolbox. It reads your code as you type, offering suggestions and automatically tidying up.- Formatting: The act of tidying up your code—fixing spacing, indentation, etc. Think of it as organizing your bookshelf.
- Linting: The act of checking your code for subtle errors or bad habits. It's like your assistant pointing out a book that's upside down.
Here's a quick reference table:
| Term | Simple Definition | Where It Shows Up |
| Go | The programming language created by Google. | You install this on your computer. |
| VS Code | A popular, free code editor from Microsoft. | This is the program you'll write code in. |
| Go Extension | A plugin for VS Code that adds Go support. | You install this from the Extensions Marketplace inside VS Code. |
gopls | The Go Language Server; the engine behind the extension. | It runs in the background, powering auto-complete and formatting. |
Quick Setup
All the tools we need are free and work on macOS, Windows, and Linux.
- Go: Download the latest version from the official Go website.
- VS Code: Download it from the official VS Code website.
Just grab the installers for your operating system and run them. The default settings are perfect.
Do It Step by Step
Let's get everything hooked up. This should only take about 10 minutes!
1. Install Go and Verify It
Our goal is to get the Go language running on your machine.
- Download and run the installer from go.dev. It’s a standard installation—just click "Next" until it's done.
- Open your terminal (Terminal on Mac/Linux, PowerShell or Command Prompt on Windows).
Type the following command and press Enter to check if it worked:
# WHY: This command asks Go to report its version, confirming it's installed correctly.
go version
You should see something like this, though your version number might be different:
go version go1.22.5 darwin/amd64
Pro Tip — If you get a "command not found" error, try closing and reopening your terminal. Sometimes it needs a fresh start to recognize newly installed programs.
2. Install VS Code and the Go Extension
Now, let's set up your workshop (the editor).
- Download and install VS Code if you haven't already.
- Open VS Code.
- On the left-hand sidebar, click the Extensions icon (it looks like four squares, with one flying off).
- In the search bar at the top, type
Go. - Find the extension named Go by the Go Team at Google and click the blue Install button.
Once it's installed, VS Code is now officially a Go editor! It might prompt you to install some extra Go tools in the bottom-right corner. Go ahead and click "Install All."
3. Enable Auto-Formatting on Save
This is the final, magic step. Let's tell VS Code to clean our files automatically.
- Open the settings. You can use the shortcut
Cmd + ,on Mac, orCtrl + ,on Windows/Linux. - In the search bar at the top of the Settings tab, type
Format On Save. - Check the box next to
Editor: Format On Save. That's it!
For good measure, let's make sure it knows to use the Go formatter for Go files.
| Setting | Default | What It Does | Recommended Value |
| Editor: Format On Save | false | When checked, automatically formats your code every time you save the file. | true (checked) |
| Editor: Default Formatter | (none) | Tells VS Code which formatter to use for different languages. | golang.go |
Now you're all set. Every time you save a .go file, it will snap into perfect shape.
Examples in Action
Talk is cheap. Let's see it work.
Example A: Messy Code Becomes Clean
- Create a new file in VS Code named
main.go. - Now, simply save the file (
Cmd + SorCtrl + S).
Copy and paste this intentionally messy code into the file. Notice the weird spacing and indentation.
// before-save.go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Poof! The Go extension, powered by gopls, instantly reformats it.
// after-save.go
package main
import "fmt"
func main() {
// WHY: The tool `gofmt` fixed the package spacing, added a tab for indentation,
// and fixed the extra spaces inside Println. All automatically.
fmt.Println("Hello, World!")
}Example B: The Linter Catches a Mistake
The Go assistant (gopls) also warns you about potential bugs. Let's declare a variable but never use it.
- You don't even need to save. A yellow squiggly line will appear under
unusedMessage. If you hover your mouse over it, you'll see a message like:unusedMessage declared but not used. This is the linter helping you write cleaner, safer code.
Add a new line to your main.go
package main
import "fmt"
func main() {
// WHY: We're creating a variable but never using it.
// Go's linter sees this as a potential mistake or leftover code.
var unusedMessage = "I am not needed"
fmt.Println("Hello, World!")
}
Here's a diagram of what happens every time you save a file:
flowchart LR
subgraph You
A[Write or paste code] --> B{Save File};
end
subgraph "VS Code Automation"
B --> C[Go Extension wakes up];
C --> D[Runs `gofmt` to format];
D --> E[Runs linter to check for issues];
end
subgraph Result
E --> F[Code is perfectly formatted ✨];
E --> G[Warnings appear for issues 🔍];
end
ASCII Fallback: You write code -> You save the file -> The Go Extension automatically runs formatters and checkers -> The final result is clean code with helpful warnings.
Common Pitfalls
If things aren't working, it's usually one of these three things:
- "Command
gonot found" in the terminal. This means Go wasn't added to your system'sPATH. The easiest fix is to restart your computer. If that doesn't work, re-run the Go installer. - Formatting doesn't happen on save. Double-check that the "Format On Save" box is checked in your VS Code settings. Also, make sure the Go extension is installed and enabled.
- A tool failed to install. If you saw an error in the bottom-right of VS Code about a tool failing to install (like
goplsorgofmt), open the command palette (Cmd/Ctrl + Shift + P), typeGo: Install/Update Tools, press Enter, check all the boxes, and click OK.
FAQ
Q: Do I have to use VS Code?
A: Nope! Other editors like GoLand or Vim have excellent Go support, but VS Code is free, popular, and a fantastic place to start. The core tool, gopls, works with many different editors.
Q: What exactly is gofmt?
A: It's the specific command-line tool that handles Go code formatting. It's famous for having no options—it formats code one way, and that's it. This prevents all arguments about style. gopls uses gofmt under the hood.
Q: Is formatting the same as compiling?
A: Not at all. Formatting is about style and readability (how the code looks). Compiling is about turning your human-readable code into a machine-readable program that can actually run.
Q: Can I change the formatting rules?
A: Generally, no. The Go community strongly believes in a single, unified style enforced by gofmt. This is considered a feature, not a limitation!
Recap & Next Steps
That's it! You've built a simple but powerful Go development environment.
- You installed the Go language.
- You set up VS Code with the official Go extension.
- You enabled automatic formatting on save, which will save you tons of time.
Now you can stop worrying about style and start building.
- Next Step: Try writing your first program! A classic "Hello, World!" is a great start.
- Then: Learn about the
go runcommand to execute your code directly from the terminal. - Explore: Look through the other features the Go extension provides, like auto-complete and function definitions on hover.
Happy building!
Assumptions: The screenshots and UI element names for Visual Studio Code are based on versions common in late 2025. The exact wording or location of a setting might change slightly over time, but the core concepts (installing the extension, enabling format on save) will remain the same.