Learn Golang
  • Learn Go
  • The Basics
    • Installing Go
    • Hello World
  • Language Features
    • Basic Input Output
    • Variable Basics
    • Control Statements
    • Loops
    • Functions
    • Recursion
  • Data Structures
    • Arrays
    • Slices
    • Maps
    • Pointers
    • Structs
  • Other Concepts
    • Sorting
    • Interfaces
    • Generics
    • Goroutines
  • References
Powered by GitBook
On this page
  1. Language Features

Variable Basics

Variables, constants, and booleans in go

PreviousBasic Input OutputNextControl Statements

Last updated 2 years ago

CtrlK
  • Variables and Values
  • Constants
  • Integer and Float Operations
  • String Operations

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

constants.go
package main

import "fmt"

func main() {
    const PI := 3.1428
    const first_president := "George Washington"
    const is_raining := true
    const complex_num := complex(2, 3) // same as complex_num := 2 + 3i
    const a_character := "S"
}

Integer and Float Operations

3.1 Arithmetic Math Operations

package main

import "fmt"
func main() {
    // golang supports the basic arithmetic operations
    fmt.Println(3 + 2)   // addition
    fmt.Println(8.3 - 2) // subtraction
    fmt.Println(9 / 3)   // division
    fmt.Println(7 * 5)   // multiplication
    fmt.Println(14 % 6)  // modular division
    x := 10
    fmt.Println((x++))   // increment x by 1
    fmt.Println((x--))   // decrement x by 1
}

3.2 The math package

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

math_operations.go
package main

import (
    "fmt"
    "math"
    )

func main() {
    fmt.Printf("%.2f\n", math.Abs(-973))   // prints: 973.00 (converts value to float)
    fmt.Printf("%.2f\n", math.Log(2.7183)) // prints: 1.00
    fmt.Printf("%.2f\n", math.Round(22/7)) // prints: 3.00
    fmt.Printf("%.2f\n", math.Pow(2, 5))   // prints: 32.00
    fmt.Printf("%.2f\n", math.Max(9, 2))   // prints: 9.00
}

String Operations

string_operations.go
package main

import (
    "fmt"
    s "strings"
    )
// above we create an alias for the "strings" import to make
// it easier to use the string operations
var pt = fmt.Println
func main() {
    // concat two string
    s1 := "My name is "
    s2 := "Kratos"
    pt(s1 + s2)
    
    // other string methods
    pt("Count: ", s.Count("some statements", "s"))
    pt("Contains substring: ", s.Contains("egregious", "reg"))
    pt("Repeat a string: ", s.Repeat("T", 7))
    pt("To Upper case: ", s.ToUpper("pirates"))
    pt("To Lower case: ", s.ToLower("CARIBBEAN"))
    pt("Index of letter (first occurance): ", s.Index("schadenfreude", "d"))
    pt("Split by character: ", s.Split("ge-ne-ra-l k-en-ob-i", "-"))
}
$ go run string_operations.go
My name is Kratos
Count:  3
Contains substring:  true
Repeat a string:  TTTTTTT
To Upper case:  PIRATES
To Lower case:  caribbean
Index of letter (first occurance):  4
Split by character:  [ge ne ra l k en ob i]