Introduction
Before writing Go code, you need to install the Go toolchain from the official site, go.dev, which provides installers for Windows, macOS, and Linux. Once installed, Go projects are typically organized using Go modules, defined by a go.mod file, which replaced the older GOPATH-based workspace model as the standard way to manage dependencies and project structure since Go 1.11.
Cricket analogy: Just as a player must first get official kit and register with the board before playing, a Go developer installs the toolchain from go.dev before writing code, then organizes projects with go.mod, the modern replacement for the old GOPATH setup.
Syntax
go version
go mod init example.com/myapp
go run main.go
go build
go installExplanation
After installing Go, go version confirms the installed version. go mod init creates a go.mod file that names the module and tracks its dependencies, freeing you from the older requirement of placing code inside a specific GOPATH directory structure. go run compiles and immediately executes a program in one step, go build compiles it into a binary in the current directory without running it, and go install compiles the binary and places it into your Go bin directory so it can be run from anywhere.
Cricket analogy: Checking go version is like checking your kit is regulation before the match; go mod init sets up your team roster and dependencies file, go run is a net practice that plays out immediately, go build prepares the squad without playing, and go install puts the player on the national reserve list to be called up anywhere.
Example
// main.go
package main
import "fmt"
func main() {
fmt.Println("Go environment is ready!")
}
// Terminal commands:
// go mod init example.com/hello
// go run main.goOutput
Go environment is ready!Key Takeaways
- Install Go from the official go.dev downloads page for your operating system.
- Verify installation with go version.
- Go modules (go.mod), introduced in Go 1.11, are the modern way to manage dependencies, replacing GOPATH-only workflows.
- go run compiles and executes a program in a single step, useful for quick testing.
- go build produces a standalone binary without running it.
- go install builds and places the executable in your Go bin directory for reuse.
Practice what you learned
1. Where should Go be downloaded and installed from officially?
2. Which command verifies that Go is installed and shows its version?
3. What file defines a Go module and its dependencies?
4. Which command compiles and immediately runs a Go program without leaving a binary in the working directory?
5. What was the older Go workspace model that Go modules largely replaced?
Was this page helpful?
You May Also Like
Introduction to Go Programming
A first look at Go, a statically typed, compiled language built for simplicity, speed, and concurrency.
Packages and Go Modules
Learn how Go organizes code into packages and manages dependencies with go.mod using the modules system.
Go Tooling: gofmt, go vet, and go build
Explore Go's built-in tools for formatting, static analysis, compiling, and running programs consistently.
History and Evolution of Go
Learn how Go was created at Google in 2007 and evolved into a widely adopted language for modern software.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics