Go
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions and Methods
-
Section 4: Concurrency
-
Section 5: Packages and Tooling
-
Section 6: More Standard Library
-
Section 7: Building Services
-
Section 8: Advanced Go
-
Section 9: Go in the Cloud-Native Ecosystem
-
Section 10: Data Structures and Algorithms in Go
-
Section 11: Testing and Deployment
-
Section 12: Practical Projects
-
Section 13: More Standard Library Practice
-
Section 14: More Practice Projects
-
Section 15: Design Patterns in Go
-
Section 16: Interview Practice
-
Section 17: Package fmt In Depth
-
Section 18: Package strings and strconv
-
Section 19: Package os and io
-
Section 20: Package time
-
Section 21: Package sort and container
-
Section 22: Package encoding
-
Section 23: Package net/http In Depth
-
Section 24: Package context
-
Section 25: Package regexp and bytes
-
Section 26: Package errors In Depth
-
Section 27: Package crypto and hash
-
Section 28: Package flag and log
-
Section 29: Package sync In Depth
-
Section 30: More Practice Exercises
-
Section 31: Go Modules and Workspaces In Depth
-
Section 32: Generics Deep Dive (Go 1.18+)
-
Section 33: Testing Package In Depth
-
Section 34: More Interview and Whiteboard Practice
-
Section 35: Package math and unicode
-
Section 36: Package path and filepath
-
Section 37: Package database/sql
-
Section 38: Package text/template and html/template
-
Section 39: Package archive and compress
-
Section 40: Lower-Level net Package
-
Section 41: Package runtime and reflect
-
Section 42: Package embed and io/fs
-
Section 43: Go Assembly and CGO Basics
-
Section 44: Building CLIs and TUIs
-
Section 45: Go Performance Tuning
-
Section 46: More Real-World Projects
-
Section 47: Go in Production
-
Section 48: Go Design Patterns
-
Section 49: Go Interfaces Deep Dive
-
Section 50: Final Practice and Review
3: The go Command-Line Tool
You've got the syntax down, but writing code in an editor is only half the battle. To actually use Go, you need to get comfortable with the go command-line tool. It's not just a compiler; it's a build system, a formatter, and a package manager all rolled into one. Instead of listing commands, let's just build something and see where the tool helps us out.
I've put together a simple utility called wordcount. It takes a filename as an argument and tells you how many words are in it. Right now, it's just a file called main.go in a folder on my desktop.
Wait, where did the executable go?
When I'm just sketching out logic, I don't want to deal with managing binary files. I just want to see if my logic works. So, I'll try this:
go run main.go
It works! I see the word count in my terminal. But here's the thing: I look in my folder, and there is no wordcount executable. I'm a bit confused—did it just run in memory? Exactly. go run compiles your code to a temporary directory and executes it immediately. It's great for rapid prototyping, but if you're handing this tool to a teammate, you can't just send them a .go file and expect them to have the Go toolchain installed.
I need a real binary. Let's try go build:
go build main.go
Now, when I check my directory, there it is: a file named main (or main.exe on Windows). I can move this file to any other machine with the same OS, and it'll run without Go being installed. One detail though—since I passed the filename main.go, Go named the binary main. That's not very descriptive. If I run go build without specifying the filename while inside a module, Go uses the module's name. Much cleaner.
Cleaning up my mess
I'll be honest: I tend to get sloppy with my indentation when I'm in "flow state." My wordcount code currently has a mix of tabs and spaces, and some weirdly placed curly braces. It's ugly. In other languages, we'd spend an hour arguing about Prettier or ESLint configurations. In Go, we just do this:
go fmt main.go
I didn't see any output in the terminal, but when I jump back into my editor, the code has shifted. It's perfectly aligned. go fmt is opinionated and non-negotiable; it ensures that every Go project looks the same, regardless of who wrote it. I've stopped fighting it and just run it instinctively before every commit.
Making it a global tool
Right now, to run my wordcount tool, I have to be in the specific folder where the binary lives, or I have to type the full path. That's annoying. I want to be able to type wordcount from any directory in my terminal and have it just work.
I'll try go install:
go install
Again, nothing seems to happen in the current folder. No binary appeared. But if I move to a completely different directory—say, my home folder—and type wordcount, it runs. What happened? go install compiles the binary and moves it into a specific folder in your GOPATH (usually ~/go/bin). As long as that folder is in your system's PATH environment variable, your Go programs become global commands on your machine.
- go run: Quick check. No permanent binary.
- go build: Creates a local binary for distribution.
- go fmt: Fixes your messy formatting automatically.
- go install: Puts the binary in your Go bin folder for global access.
📋 Practical Task
Building and Installing a Global Directory-Size Calculator
Your task is to move beyond go run and integrate a tool into your system's environment. Create a small program called dirsize that prints the current working directory's path and a hardcoded string saying "Calculating size..." (you don't need to implement the actual size logic, just the CLI structure).
Requirements:
- Initialize a module named
dirsizeusinggo mod init dirsize. - Write the code in
main.go. - Use
go fmtto ensure your code meets the official Go standards. - Use
go installto install the tool globally. - Verify the installation by opening a new terminal window, navigating to a completely different folder, and executing the
dirsizecommand.
There are no comments for now.