Skip to main content

Command Palette

Search for a command to run...

DAY 2 - Variables, Constants, Datatypes in GO

Published
4 min read

Variables

In Go, variables are used to store values of different types.

TypeDescription
intStores integers (whole numbers)
float32Stores floating-point numbers
stringStores text values
boolStores boolean values (true or false)

Different Ways of Declaring Variables

var variable_name type = value
var variable_name = value
variable_name := value
  • The 2nd and 3rd are inferred types, meaning Go automatically determines the variable type based on the assigned value.

You can also declare variables without assigning values. In such cases, Go assigns default zero values.

Example:

var a string
var b int
var c bool

fmt.Println(a)  // ""
fmt.Println(b)  // 0
fmt.Println(c)  // false

Difference Between var and :=

Featurevar:=
ScopeCan be used inside and outside of a functionCan be used only inside a function
DeclarationCan declare without initializingMust declare and assign in the same line

Declaring Multiple Variables

var a, b, c int = 3, 4, 5

Constants in Go

If a variable’s value should never change, use const.

const TOTAL = 10

Tip: Constants are usually written in uppercase for easy recognition.

Multiple Constant Declaration

const (
    TOTAL   = 5
    PRODUCT = "Apples"
    MESSAGE = "Buy some apples"
)

Data Types in Go

Go has three basic data types:

  1. Numeric → integers, floating-point numbers, and complex types
  2. Boolean (bool) → represents true or false
  3. String → represents text data

Example:

var a string = "hello"
var b int = 10
var c bool = false
var d float32 = 10.5

fmt.Println(a)

⚠️ Go is statically typed, meaning the type of a variable is known at compile time and cannot change.


Integers in Go

There are two types of integers:

  1. Signed integers (int)
  2. Unsigned integers (uint)

By default, if you declare a variable without specifying its type, Go assumes it as int.


Arrays in Go

Arrays are collections of elements with fixed length.

Declaring an Array

var arr_name = [size]datatype{values}

Example:

package main

import "fmt"

func main() {
    var arr1 = [3]int{3, 5, 7}
    var arr2 = [...]int{1, 2, 3, 4, 5}
    // Here "..." means the compiler infers the length of the array

    fmt.Println(arr1, len(arr1), cap(arr1))
    fmt.Println(arr2, len(arr2), cap(arr2))

    // Accessing elements
    fmt.Println(arr1[1], arr1[len(arr1)-1])
}

Output:

[3 5 7] 3 3
[1 2 3 4 5] 5 5
5 7

For arrays, capacity always equals length.
If you create an empty array, all elements are initialized with zero values.

Example:

var arr3 = [3]int{}
fmt.Println(arr3)

Output:

[0 0 0]

If you insert more elements than declared size, Go throws an error.

var arr4 = [3]int{5, 6, 7, 8}

Error:

index 3 is out of bounds (>= 3)

Slices in Go

Slices are dynamic arrays — they can grow or shrink as needed.

slice1 := []string{"a", "b", "c"}

Slices have both length and capacity, which can change dynamically.

Example:

package main

import "fmt"

func main() {
    // Creating Slices
    slice1 := []string{"a", "b", "c"}
    fmt.Println(slice1, len(slice1), cap(slice1))

    // Appending elements to slice
    slice1 = append(slice1, "d", "e")
    fmt.Println(slice1, len(slice1), cap(slice1))

    // Appending slice to slice
    slice3 := []int{1, 2, 3, 4, 5}
    slice4 := []int{6, 7, 8}
    slice3 = append(slice3, slice4...) // "..." copies all elements from slice4
    fmt.Println(slice3)

    // Creating slice from array
    arr5 := [6]int{3, 5, 7, 8, 2, 5}
    slice2 := arr5[1:3]
    fmt.Println(slice2)

    // Creating slice using make([]type, length, capacity)
    // If capacity isn’t specified, it defaults to the length.
    slice5 := make([]int, 5, 10)
    fmt.Println(slice5) 

}

Output:

[a b c] 3 3
[a b c d e] 5 6
[1 2 3 4 5 6 7 8]
[5 7]
[0 0 0 0 0]

Slices provide flexibility compared to arrays — they automatically manage capacity and length.

More from this blog

G

GoSprint90

16 posts

Welcome to GoQuest90 — a 90-day journey from absolute beginner to confident Go developer.