Go Things
Compilation
to generate object file go :
to generate assmebly :
Named return
Data Types
byte : alias for uint8 rune : alias for int32
If Short Hand
Defer
Defer : A defer statement defers the execution of a function until the surrounding function returns. The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.
Sprint
Switch
Switch Operations
Tagless switch
Function Closure
Go functions may be closures. A closure is a function value that references variables from outside its body. The function may access and assign to the referenced variables; in this sense the function is "bound" to the variables. hence the function becomes static in some sense and its lifetime is that of the variable it gets binded to. also the internal variable of the func also lives as long as the binded variable
panic and recover
Earlier, we created a function that called the panic function to cause a runtime error. We can handle a runtime panic with the built-in recover function. recover stops the panic and returns the value that was passed to the call to panic . We might be tempted to recover from a panic like this:
But the call to recover will never happen in this case, because the call to panic imme‐ diately stops execution of the function. Instead, we have to pair it with defer :
Iota basic example
The iota keyword represents successive integer constants 0, 1, 2,…
It resets to 0 whenever the word const appears in the source code,
and increments after each const specification.
This can be simplified to
Here we rely on the fact that expressions are implicitly repeated in a parenthesized const declaration – this indicates a repetition of the preceding expression and its type.
Start from one To start a list of constants at 1 instead of 0, you can use iota in an arithmetic expression.
Skip value You can use the blank identifier to skip a value in a list of constants.
Complete enum type with strings [best practice] Here’s an idiomatic way to implement an enumerated type:
create a new integer type,
list its values using iota,
give the type a String function.
type Direction int
In use:
Last updated