Skip to content

Go Language Overview

What is Go?

Go (often called Golang) is an open-source, statically typed, compiled programming language designed at Google. Announced in 2009, it was created to improve programming productivity, especially for large-scale software, networked services, and multicore processors.

It draws inspiration from languages like C for performance but aims for simplicity and readability akin to Python. Key features include automatic memory management (garbage collection) and built-in support for concurrent programming.

Why Go?

Go offers several compelling advantages:

  • Simplicity & Readability: Go has a clean, minimal syntax, making it relatively easy to learn, read, and maintain, even in large teams.
  • Performance: As a compiled language, Go applications compile directly to machine code, resulting in fast execution speeds comparable to C or C++. It also boasts fast compilation times.
  • Concurrency: Go makes concurrent programming straightforward with lightweight goroutines and channels, allowing efficient use of multi-core processors for scalable applications.
  • Strong Tooling: Go comes with a robust standard library and excellent built-in tools for formatting (gofmt), testing (go test), documentation (godoc), and dependency management.
  • Scalability: Its efficiency, concurrency model, and suitability for microservices and cloud-native applications make it excellent for building systems that need to handle significant load.
  • Cross-Platform: Go easily cross-compiles to different operating systems and architectures, simplifying deployment.

Installation

You can install Go on Windows, macOS, and Linux. The primary method is downloading an installer or archive from the official Go website.

Official Download

  1. Visit the official Go downloads page: go.dev/dl/
  2. Download the appropriate package for your operating system (Windows .msi, macOS .pkg, Linux .tar.gz).

Installation Steps

  1. Run the downloaded MSI installer.
  2. Follow the prompts. By default, Go is installed in C:\Program Files\Go (or C:\Go in some versions) and the C:\Go\bin directory will be added to your system PATH environment variable.
  3. Open a new Command Prompt or PowerShell window (important to apply PATH changes) and verify the installation:
    Terminal window
    go version
    This should print the installed Go version.

Verify Installation

Regardless of the method, always verify your installation by opening a new terminal or command prompt and running:

Terminal window
go version

Variables, Types, and Constants

Understanding how Go handles data through variables, types, and constants is fundamental.

Variables

Variables are used to store data values. Go is statically typed, meaning a variable’s type is checked at compile time.

Declaration

You can declare variables using the var keyword.

Declare with type:

var age int // Declares an integer variable named 'age'
age = 30 // Assigns a value

Declare and initialize:

var name string = "Alice" // Declares and initializes a string variable

Type inference: Go can infer the type if an initial value is provided.

var isReady = true // Go infers 'isReady' is of type bool

Multiple variables:

var x, y int = 10, 20 // Declare multiple variables of the same type
var (
city string = "London"
country string = "UK"
) // Block declaration

Short Variable Declaration (:=)

Inside functions, you can use the := short assignment statement for implicit declaration and initialization. This is the most common way to declare variables within functions.

func main() {
message := "Hello, Go!" // Declares 'message' as string and initializes it
count := 1 // Declares 'count' as int and initializes it
isValid := false // Declares 'isValid' as bool and initializes it
// Cannot use := outside a function
// port := 8080 // This would cause a compile error at the package level
}

Note: := can only be used when at least one variable on the left side is newly declared.

Zero Values

Variables declared without an explicit initial value are given their zero value:

  • 0 for numeric types (int, float, etc.)
  • false for booleans
  • "" (empty string) for strings
  • nil for pointers, functions, interfaces, slices, channels, and maps.
var i int // i == 0
var f float64 // f == 0.0
var b bool // b == false
var s string // s == ""
var p *int // p == nil

Data Types

Go has several built-in data types:

Basic Types

  • Boolean: bool (true or false)
  • String: string (sequence of bytes, usually representing UTF-8 encoded text)
  • Integer:
    • Signed: int, int8, int16, int32, int64
    • Unsigned: uint, uint8 (alias byte), uint16, uint32, uint64, uintptr
    • int, uint, and uintptr are platform-dependent sizes (32 or 64 bits)
  • Floating-Point: float32, float64
  • Complex: complex64, complex128

Composite Types

Array: [n]T (fixed-size sequence of type T)

var numbers [5]int // Array of 5 integers, initialized to 0

Slice: []T (dynamically-sized view into an underlying array of type T)

letters := []string{"a", "b", "c"} // Slice of strings

Map: map[K]V (unordered collection of key-value pairs)

ages := map[string]int{"Alice": 30, "Bob": 25} // Map string keys to int values

Struct: struct (collection of named fields)

type Person struct {
Name string
Age int
}
p := Person{Name: "Charlie", Age: 35}

Type Conversion

Go requires explicit type conversions:

var i int = 42
var f float64 = float64(i) // Explicit conversion from int to float64
var u uint = uint(f) // Explicit conversion from float64 to uint (truncates)

Constants

Constants are values that are fixed at compile time. They are declared using the const keyword.

Basic constant declaration:

const Pi float64 = 3.14159 // Typed constant
const World = "世界" // Untyped string constant
const Truth = true // Untyped boolean constant
const (
StatusOK = 200
StatusError = 500
) // Grouped constants
// iota is a special constant generator for sequential values
const (
C0 = iota // C0 == 0
C1 = iota // C1 == 1
C2 = iota // C2 == 2
)

Typed vs. Untyped Constants:

const UntypedInt = 1000
const TypedInt int = 10
var f float64 = UntypedInt // OK: UntypedInt can become float64
var i int = UntypedInt // OK: UntypedInt can become int
// var f2 float64 = TypedInt // Error: cannot use TypedInt (type int) as type float64
var f2 float64 = float64(TypedInt) // OK: Explicit conversion required