Go Part 2: Basics of variables & pointers

Pointers

  • A pointer is an address to data in memory

  • & operator returns the address of a variable/function

  • * operator returns the data at an address (dereferencing)

The ampersand (&) operator, if we put that in front of a variable, the name of a variable, that will return us the address of that variable. The star(*) operator goes the other way. If we put that in front of a Pointer, it will return the data to that address. So, it's important to understand this ampersand(*) operator and the star(*) operator are opposites of one another.

var x int = 1
var y int // default value 0
var ip *int // ip is pointer to int

ip = &x // ip now points to x
y = *ip // y is now 1
  • new() function creates a variable and returns a pointer to the variable

  • variable is initialised to 0.

ptr := new(int) // ptr is pointer to int
*ptr = 3 //Set value to pointer (address specified by above new func)

Variable Scope

  • The places in code where a variable can be accessed
var x = 4

func f(){
    fmt.Printf("%d", x)
}

func g(){
    fmt.Printf("%d", x)
}
func f(){
    var x = 4
    fmt.Printf("%d", x)
}

func g(){
    fmt.Printf("%d", x) // This will throw error
}

Blocks

  • A sequence of declarations and statements within matching brackets, {}

    • Including function definitions
  • Hierarchy of implicit blocks

  • Universe block - all go source

  • Package block - all sources in a package

  • File block - all sources in a file

  • if for switch - all code inside the statement

  • Clause in switch or select - Individual clauses get a block

Lexical scoping

  • Go is a lexically scoped using blocks

Deallocating Space

  • When a variable is no longer needed, it should be deallocated

    • Memory space is made available
  • Otherwise, we will eventually run out of memory

  • Stack vs Heap

    • The stack is an area of memory that is dedicated to function calls,

      primarily dedicated to function calls. So every time you call a function there can be variables that you define in that function and generally, they go into the stack. They are allocated in the stack area of the memory and they're deallocated. If they're allocated in the stack, they are deallocated

      automatically when the function completes - THIS IS IN REGULAR LANGUAGES

    • Now the heap, on the other hand, is a persistent region of memory where when we allocate something on the heap it doesn't go away just because the function that allocated it is complete, that heap memory, we have to explicitly deallocate it somehow in another language. Now Go does a tweak on this

Garbage Collection

  • In interpreted languages, this is done by the interpreter

    • Java Virtual Machine

    • Python interpreter

  • It keeps track of these pointers and it determines when a variable is not in use anymore and when it is. Once it determines that a variable is definitely not used, there are no more pointers, and no more references to that variable then the garbage collector deallocates it. Only when all references are gone.

  • Easy for programmer

  • Slow (need an interpreter)

Garbage Collection in Go

  • Go is a compiled language which enables garbage collection

  • Go is a compiled language that has garbage collection built into it. So, that is a unique feature of Go which is really nice. So as a result, the Go compiler can figure out when to follow these points to some extent and figure out when this point you're still in use.

  • So, garbage collection in Go allows two things.

    • One thing is, that it will actually allocate stuff on the heap and the stack itself.

    • The Go compiler, it'll put code in there at compile-time, it'll figure out this needs to go heap, this needs to go to the stack and it'll garbage collect appropriately. So, if it's on the heap it will garbage collected appropriately will see when all the pointers are gone and it'll determine when it can be deallocated.

  • Now, there is a downside because the act of garbage collection does take some time. Right. So, there's a performance hit but it's a pretty efficient implementation and garbage collection is so darn useful is probably worth it to put it in Go. So, that's a trade-off that Go makes. It slows things down a little bit but it is a great advantage because it makes programming a lot easier. We don't have to go as far as using a full-on interpreter like we would in an interpreted language.

Comments

  • Comments are text for understandability

  • Ignored by the compiler

  • Single line comments ( // This is a comment)

  • Block comments (/* multi-line */)

Ints, Floats, Strings

  • Type Conversions

    • Most binary operations need operands of the same type

    •   var x int32 = 1
        var y int16 = 2
      
        x = y // will throw an error
      
    • Convert type with T() operation

      •   x = int32(y)
        
  • Floating Point

    • float32 - 6 digits precision

    • float64 - 15 digits precision