7. Go - Variable Declaration

Variable Declarations:

Declare variable types using "var"

// Single Variable Declaration
var age int
var names string
var width float64


// Pointers Variables
var agePointer *int
var namesPointer *string
var widthPointer *float64


Struct Declaration and Use:

    type Person struct {
        gender string
        age int
    }
   
    todd := Person{
        gender: "male",
        age: 5,
    }

    fmt.Println(todd.gender)


Declaring multiple variables:

var (
    age = 5
    names = "todd"
    baldQuestion = false
)


Constants, variables that cannot be changed. They're immutable.

// Declaring single constants
const age int
const name string

// Declaring multiple constants
const (
    a = 5
    b = 6
    c = "string"
)


Short Declaration Operator uses the " := " symbol. 

Lets declare stuff without typing var. 

Can only use within a function's scope, can't be used outside of a function. Declare variables outside of functions using "var"

Example:

banana := "yellow"
size := 5
green := false


Comments

Popular posts from this blog

2. FreeCodeCamp - Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges

20. Data Analytics - Analyze Data to Answer Questions - Week 1

3. Algorithms - Selection Sort