Kanngongkanngoen

Go Basics: Installation, Packages, Variables, Collections, and Loops

💡 Key takeaways

Learn the Go fundamentals covered in this guide, including setup, imports, formatted input and output, variables, collections, and iteration.

This guide introduces Go setup and core syntax. Install a Go version supported by your project and operating system, then check the installation with go version and inspect environment settings with go env. Package-manager and archive-based installation steps differ by platform; use the current official installation instructions for a new setup.

Packages and formatted input/output

A Go file declares a package and imports the packages it uses. An import alias can shorten a package name; the blank identifier _ can be used for a package’s initialization side effects, though it should not be used to hide needed values. The fmt package provides functions such as Print, Println, Printf, and Scan. Common formatting verbs include %v for a value, %s for a string, %d for an integer, %t for a boolean, %#v for a Go-syntax representation, and %T for a type.

Variables and zero values

Declare a variable with var, define a constant with const, or use := for short declarations inside a function. Go assigns each declared variable its type’s zero value when no initial value is supplied. Choose explicit names and types that make the program easy to understand.

Arrays, slices, maps, and iteration

An array has a fixed length. A slice is a flexible view over an underlying array and can grow when used with append; len reports its current length. Go strings are UTF-8 byte sequences, so byte length and the number of Unicode code points are not always the same. The unicode/utf8 package can count runes when that is the intended measure.

A map stores key-value pairs. A for loop can repeat work, and for range can iterate over arrays, slices, maps, strings, and channels. Map iteration order is not guaranteed. Editor choices such as VS Code, GoLand, and their extensions are optional; tool setup changes over time, so follow current Go and editor documentation.

Contact

For questions about the site, use the contact option below.

Contact on LINE

LINE contact QR code

Video and references

📋 Plan your finances and manage taxes in one place

See your income, expenses, balance sheet, and tax plan together so you can keep track of eligible deductions.

Start your full financial plan →
KGK

Kanngongkanngoen Editorial Team

We create clear educational content about personal finance, investing, and debt management, with an emphasis on accuracy and transparency.