Fundamentals

Variables and Data Types

Go is statically and strongly typed.

Variables

The var statement declares one or more variables. Like function parameters, the type is specified at the end.

It can be used at both the package level and within functions.

package main

import "fmt"

var golang, python, c bool

func main() {
    var rust int
    fmt.Println(rust, golang, python, c)
}

Variables with Initializers

A var declaration can include initializers, one per variable.

If an initializer is provided, the type can be omitted. In such cases, the variable’s type is inferred from the initializer.

package main

import "fmt"

var i, j int = 1, 2

func main() {
    var golang, python, rust = true, false, "yes"
    fmt.Println(i, j, golang, python, rust)
}

Short variable declarations

When you are inside a function, you can use a shortcut := to declare and assign a variable all at once.

Go is smart enough to automatically infer the data type.

package main

import "fmt"

func main() {
    var i, j int = 1, 2
    k := 3
    golang, python, rust := true, false, "yes"

    fmt.Println(i, j, k, golang, python, rust)
}
Outside a function, declarations must begin with a keyword such as var or func. As a result, the := short variable declaration syntax is not allowed at the package level.

Zero values

If you use the var keyword but don't assign a value right away (like var name int), Go will safely set it to a "zero value" (which is 0 for integers) rather than leaving it undefined.

The zero value is:

  • 0 for numeric types,
  • false for the boolean type, and
  • "" (the empty string) for strings.
package main

import "fmt"

func main() {
    var i int
    var f float64
    var b bool
    var s string
    fmt.Printf("%v %v %v %q\n", i, f, b, s)
}

The Core Data Types

As a beginner, you will mostly work with these four basic types:

  • string: For text (e.g., "Hello")
  • int: For whole numbers (e.g., 10)
  • float64: For decimal numbers (e.g., 10.5)
  • bool: For true/false values (e.g., true)
main.go
package main

import "fmt"

func main() {
    // 1. The Standard Way: Explicitly stating the type
    var name string = "Alice"
    var age int = 25

    // 2. The Short Way: Go infers the type automatically
    height := 5.8       // inferred as float64
    isStudent := true   // inferred as bool

    fmt.Println("Name:", name)
    fmt.Println("Age:", age)
    fmt.Println("Height:", height)
    fmt.Println("Is Student:", isStudent)
}

Type Conversions

In Go, type conversion is explicit. You must clearly specify when converting a value from one type to another.

var i int = 42
var f float64 = float64(i)
var u uint = uint(f)
Conversions must be explicit in Go.

Type Inference

In Go, you don’t always need to explicitly specify a variable’s type. When a variable is declared with an initializer, Go determines its type automatically based on the assigned value.

This applies when using both:

  • short declaration syntax (:=)
  • var with an initializer
var i int
j := i // j gets the same type as i (int)

If the assigned value already has a defined type, the new variable adopts that type.

Untyped Constants Behavior

When the assigned value is an untyped constant, Go decides the type based on the value’s form and precision.

i := 42           // inferred as int
f := 3.142        // inferred as float64
g := 0.867 + 0.5i // inferred as complex128

Common Doubts

Copyright © 2026