Skip to content

Instantly share code, notes, and snippets.

@Lyoness
Last active June 27, 2022 15:37
Show Gist options
  • Save Lyoness/bcf934a568b703d4c230295534f8d1d4 to your computer and use it in GitHub Desktop.
Save Lyoness/bcf934a568b703d4c230295534f8d1d4 to your computer and use it in GitHub Desktop.

Language:

Struct: with inline comments for structs and memory memory padding / boundaries https://play.golang.org/p/hZRNwaKrMb we want to fit values based on size inside same byte boundaries. Memory Padding ift's 2 byte value - 2 byte alignment 4 byte value - 4 byte alignment (compiler does this) Bools lose their 0 value b/c of padding.

consider consequences at scale

Memory optimization: to push memory padding to end, order type largest value to least (32, 16, bool) https://play.golang.org/p/ba29y0n-7m

Go doesn't use constructors, we use strcut literals

https://github.com/ardanlabs/gotraining/blob/master/topics/language/struct_types/README.md

Anonymous Struct Type: https://play.golang.org/p/x-Dpp9Ts_U <-#L14

Compiler error messages are rad. Make them your friends.

Strong Types

Cannot assign identical types to eachother in #golang no implicit converstion between variables in Go

Code Review

Declare, create and initialize struct types (Go Playground)
Anonymous struct types (Go Playground)
Named vs Unnamed types (Go Playground)

Struct type Exercise

Part A: Declare a struct type to maintain information about a user (name, email and age). Create a value of this type, initialize with values and display each field.

Part B: Declare and initialize an anonymous struct type with the same three fields. Display the value.

Template (Go Playground) | Answer (Go Playground)


POINTER MECHANICS

The Stack, the Heap, Stackframes, func calls

Memory can be in 3 places: data segment stack heap

stack- every single goroutine is given its own stack. given its own peice of memory (starting at 2k) in the stack how many function calls deep do you go d in golang stack is memory that is self-cleaning (no GC) memory wasted in the software we write. - OS threads running goroutines stack memory in goroutines is private memory. cannot be shared b/t goroutines

to share memory, we use shared. heap is for memory to be shared.

Every single func call requires a piece of the stack. The stack frame (special registers needed)

when we call main, we allocate the space needed for stack frame for main stacks go down. memory can only be allocated on the stack in golang if the value is known at compile time. (if does not know value at compile time, cannot be on stack) ]

program boundaries think of var as a value throwing a piece of data over program boundary, but it must be a value of same type. <- data integrity (what's in the (private stack) box, type, value

stack frame memory is left alone.

every function call requires a stack frame needed by compiler.

Zero value - memory gets initialized on the way down. (why the stack is self-cleaning)

Pass by value versus pass by reference Value can be a memory address need a variable than can store the address for every var declared in system, you are giving a pointer type.

pointer variables only ever store and contain addresses (all pointer variables are always the same size and store same kind of value)

sharing values so we can read it and write it to that memory in golang you cannot change the value of a pointer variable (only pkg unsafe) no pointer arithmetic

stack frames

heap

can only access things on the heap through a *pointer escape analysis try to use the stack whenever you can,


Garbage Collection

Black, White, Grey

Stop the World

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment