- Go is Strictly/Strong Typed
- Tpyes checked at compile time
- Types can't be mixed
- Introduced in Go, keeping in mind the problems encountered in C / C++
Note : Strictly Typed e.g. if i is defined as int, one can only interact with it as an integer type.
-
Go has Typed and Untyped
- Untyped e.g. x := 33.40
- Typed e.g. y:=int(42)
-
Attempting to do z = x + y, as defined above, will throw exception "invalid operation: x + y (mismatched types float64 and int64) "
-
As long as something is untyped it can be combined with other untyped variable
-
Go is Pass by value
- Go does inlining while reading the code from top to bottom for better performance
- There's no while or do while loops in Go
- Is a character
- In Go Rune is alias for int32 number
- Like 100 0001 in binary represents "A"
- Rune represents int32
- Rune is made of 32 0s & 1s or 4 bytes (4*8) = 32
- UTF-8 is a 4 byte coding scheme
- Map
- Slice: dynamica, doesn't need definition like []
- Array : non dynamic, need definition like [x]
- Channel
- Struct is a sequence of named elements called fields
- Each of which has a name and a type
- Field names may be specified explicitly (IdentifierList) or implicitly (AnonymousField)
- Within a struct, non-blank field names must be unique
- A field declared with a type but no explicit field name is an anonymous field, also called an embedded field or an embedding of the type in the struct. An embedded type must be specified as a type T or as a pointer to a non-interface type name *T and T itself may not be a pointer type.
- The unqualified type name acts as the field name
- It's a aggregate type, i.e. collecting bunch of other types in list of fields e.g. first string last string age int
- instead of creating a whole class just for this, a struct can be created for this
-
Encapsulation
- state ("fields")
- behaviour ("method")
- exported / un-exported
-
Reusability
- inheritence ("embedded types")
-
Polymorphism
- interfaces
-
Overriding
- Promotion
Type of Go function that takes multiple inputs using "..." , in short : variadic functions are the functions that can have n number of parameters of a type, this is done by putting "..." in front of the type
- gofmt formats the go code
- Tabs used for the Indentation are ignored
- No line length limit
- can be done either by doing /* */ or // for single line comments
- pacakges must have comments detialing out the use case for the package
- Programs that modify data being simultaneously accessed by multiple goroutines must serialize such access
Marshal vs Unmarshal vs Encode vs Decode
- Marshal and UnMarshal deals with the string or slice of byte within the application
- Have nothing to do with the external data
-
UnMarshal is used for parsing the JSON data and stores the value in a pointer
-
Marshal encodes the data
-
Encode writes the value streaming out of the application
-
Decode reads the value streaming into the application
- avoid using types, e.g. var x int, best is to leave out the type that is more idiomatic
-
Receivers in the Method are of two type
- value receiver
- Value Type
- Pointer Type
- Pointer receiver
- Pointer Type
- value receiver
Another way to represent this
Receivers Values (t T) T and *T (t *T) *T
- Waits for a collection of goroutines to finish
- Main go routine calls Add to set the number of goroutines to wait for
- Each go routines runs and calls Done when finished
- Wait can be used to block go routines have finished
- Is composition of independently executing processes
- Is the simultaneous execution of (possibly) related computation
- It's the first program that executes
- Can be used for setting very first configuration for the page
- structure defining in/out for set of channels