Variable Basics

Variables, constants, and booleans in go

Variables and Values

Variables can be declared using the var keyword as follows

variables.go
package main

import (
    "fmt",
    "reflect"
    )
func main() {
    // STRINGS
    var name = "John" // a string variable
    
    // we can initialize multiple variables as follows
    var first_name, last_name string = "John", "Adams"
    fmt.Println(first_name, last_name) // prints "John" "Adams"
    
    // INTEGERS
    // we can tell go what the type of the varibale is during declariation
    var age int = 42
    // We can use the := shortcut to write the line above
    age := 42
    
    // FLOATS
    // types are automatically infered in golang if not specified
    var gpa = 3.5 // gpa will have type "float64"
    var final_gpa float64 = 3.75
    fmt.Println(reflect.TypeOf(gpa)) // prints "float64"
    
    // BOOLEANS
    // variables can also be booleans
    var is_alive = true
    var door_open bool = false
    
    // NULL
    var no_value = nil
    
    // COMPLEX
    complex_num := complex(2, 6)
    complex_num2 := 3 + 5i
    
    // BYTE
    var some_val byte = "😂"
    
    // DEFAULT VALUES
    // if a vaiable is declared with no inital value, a value will be auto
    // assigned to it. For example,
    var num_of_houses int; // will have a value: 0
    var is_human bool; // will have have value: false
    var user_name string; // will have value: ""
    var height float64; // will have value: 0.0
}

Constants

Constants work similar to variables, except they are immutable

Integer and Float Operations

3.1 Arithmetic Math Operations

3.2 The math package

Golang also has a math package which provides various math operations including Log(), Asin(), Round(), Abs(), etc

String Operations

Last updated