Day 2 50 min beginner

Variables and Data Types

Learn how to declare variables and work with Go's basic data types

Learning Objectives

  • Declare variables using var and short declaration syntax
  • Understand Go's basic data types
  • Work with zero values and type inference
  • Use constants for fixed values

Variables in Go

Variables are containers that store data. In Go, every variable has a specific type that determines what kind of data it can hold.

Static Typing
Go is statically typed, meaning variable types are determined at compile time. This catches many errors before your program runs!

Declaring Variables

Go provides several ways to declare variables.

Using var Keyword

The most explicit way to declare a variable:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package main

import "fmt"

func main() {
    var name string = "Gopher"
    var age int = 25
    var isActive bool = true
    
    fmt.Println(name, age, isActive)
}

Type Inference

Go can infer the type from the value:

main.go
1
2
3
var name = "Gopher"    // Go infers string
var age = 25           // Go infers int
var price = 19.99      // Go infers float64

Short Declaration (:=)

Inside functions, you can use the short declaration syntax:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package main

import "fmt"

func main() {
    name := "Gopher"      // Short declaration
    age := 25
    isActive := true
    
    fmt.Println(name, age, isActive)
}
Important
The := syntax can only be used inside functions. At package level, you must use var.

Multiple Variable Declaration

Declare multiple variables at once:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Multiple variables of same type
var x, y, z int = 1, 2, 3

// Multiple variables of different types
var (
    name    string = "Gopher"
    age     int    = 25
    balance float64 = 100.50
)

// Short declaration for multiple variables
a, b, c := 1, "hello", true

Zero Values

In Go, variables declared without an initial value get a zero value:

Type Zero Value
int, int8, int16, int32, int64 0
uint, uint8, uint16, uint32, uint64 0
float32, float64 0.0
bool false
string "" (empty string)
pointer, slice, map, channel, function, interface nil
main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package main

import "fmt"

func main() {
    var i int
    var f float64
    var b bool
    var s string
    
    fmt.Printf("int: %d, float: %f, bool: %t, string: %q\n", i, f, b, s)
    // Output: int: 0, float: 0.000000, bool: false, string: ""
}

Basic Data Types

Go has several built-in data types organized into categories:

mindmap root((Go Types)) Numeric int, int8, int16, int32, int64 uint, uint8, uint16, uint32, uint64 float32, float64 complex64, complex128 Boolean bool String string Other byte (alias for uint8) rune (alias for int32)

Integer Types

Type Size Range
int8 8 bits -128 to 127
int16 16 bits -32,768 to 32,767
int32 32 bits -2B to 2B
int64 64 bits Very large range
int Platform dependent 32 or 64 bits
uint8 8 bits 0 to 255
uint16 16 bits 0 to 65,535
uint32 32 bits 0 to 4B
uint64 64 bits Very large range
main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package main

import "fmt"

func main() {
    var a int = 42
    var b int8 = 127
    var c uint = 100
    var d int64 = 9223372036854775807
    
    fmt.Printf("a: %d, b: %d, c: %d, d: %d\n", a, b, c, d)
}
Which int to use?
Use int for most cases - it’s the most common and efficient. Use specific sizes (int8, int32, etc.) only when you need to control memory usage or match external data formats.

Floating-Point Types

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package main

import "fmt"

func main() {
    var f32 float32 = 3.14159
    var f64 float64 = 3.141592653589793
    
    fmt.Printf("float32: %f\n", f32)
    fmt.Printf("float64: %.15f\n", f64)
}
  • float32 - 32 bits, ~7 decimal digits precision
  • float64 - 64 bits, ~15 decimal digits precision (recommended default)

Boolean Type

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package main

import "fmt"

func main() {
    var isGo bool = true
    var isFun bool = true
    
    fmt.Println("Go is awesome:", isGo && isFun)
}

String Type

Strings in Go are immutable sequences of bytes (usually UTF-8 text):

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
package main

import "fmt"

func main() {
    greeting := "Hello, Go!"
    multiline := `This is a
    multi-line string
    using backticks`
    
    fmt.Println(greeting)
    fmt.Println(multiline)
    fmt.Println("Length:", len(greeting))
}

Byte and Rune

  • byte - alias for uint8, represents a single byte
  • rune - alias for int32, represents a Unicode code point
main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package main

import "fmt"

func main() {
    var b byte = 'A'          // ASCII value 65
    var r rune = 'δΈ–'          // Unicode code point
    
    fmt.Printf("byte: %c (%d)\n", b, b)
    fmt.Printf("rune: %c (%d)\n", r, r)
}

Constants

Constants are values that don’t change. Use the const keyword:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package main

import "fmt"

const Pi = 3.14159
const (
    StatusOK    = 200
    StatusError = 500
)

func main() {
    const greeting = "Hello"
    
    fmt.Println(Pi)
    fmt.Println(StatusOK, StatusError)
    fmt.Println(greeting)
}

iota - The Constant Generator

iota generates sequential integer constants:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import "fmt"

const (
    Sunday = iota    // 0
    Monday           // 1
    Tuesday          // 2
    Wednesday        // 3
    Thursday         // 4
    Friday           // 5
    Saturday         // 6
)

const (
    _  = iota             // Skip 0
    KB = 1 << (10 * iota) // 1024
    MB                    // 1048576
    GB                    // 1073741824
)

func main() {
    fmt.Println("Wednesday is day:", Wednesday)
    fmt.Printf("1 KB = %d bytes\n", KB)
    fmt.Printf("1 MB = %d bytes\n", MB)
}

Type Conversion

Go requires explicit type conversion (no automatic conversion):

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package main

import "fmt"

func main() {
    var i int = 42
    var f float64 = float64(i)    // int to float64
    var u uint = uint(f)          // float64 to uint
    
    fmt.Printf("int: %d, float64: %f, uint: %d\n", i, f, u)
    
    // String conversion
    var num int = 65
    var char string = string(num)  // Converts to "A" (ASCII)
    fmt.Println(char)
}
No Implicit Conversion
Go does not automatically convert between types, even between int and int64. You must explicitly convert using Type(value).

Printf Format Verbs

Use fmt.Printf with format verbs for formatted output:

Verb Description
%d Decimal integer
%f Floating-point
%s String
%t Boolean
%v Default format
%T Type of value
%q Quoted string
%c Character (rune)
%b Binary
%x Hexadecimal
main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package main

import "fmt"

func main() {
    name := "Gopher"
    age := 10
    height := 1.75
    
    fmt.Printf("Name: %s\n", name)
    fmt.Printf("Age: %d years\n", age)
    fmt.Printf("Height: %.2f meters\n", height)
    fmt.Printf("Type of name: %T\n", name)
    fmt.Printf("Default format: %v, %v, %v\n", name, age, height)
}

Summary

Today you learned:

  • How to declare variables using var and :=
  • Go’s basic data types (integers, floats, bools, strings)
  • Zero values and type inference
  • How to use constants and iota
  • Explicit type conversion in Go
  • Printf format verbs for output

Practice Exercise

  1. Create variables of different types and print them
  2. Experiment with zero values - declare variables without values
  3. Create a set of constants for HTTP status codes using iota
  4. Practice type conversion between int and float64

Next Steps

Tomorrow, we’ll explore operators and expressions - how to perform calculations and comparisons in Go!

Now, complete the quiz below to test your knowledge and unlock Day 3!

Quiz

Complete this quiz with a minimum score of 80% to mark Day 2 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!