Variables
Variables
Create variables with var
or with :=
.
The convention is to declare variables with :=
and not var
.
BUT, you can't explicitly state the variable type. (Ex: a int := 5
)
All Go types come with a "null value" (not nil
) which is set in a variable if no other value is explicitly set.
For instance:
int => 0
float64 => 0.0
string => ""
bool => false
pointer => nil
Variable types can be inferred from values.
In Go you cannot mix types in calculations.
Declare multiple variables in one line
But they will all be of the same type. (You cannot specifiy the type for each one)
Single vs Double quotes
In Go there is a different when using '
vs "
.
Single Quotes
Should be used to designate Rune
or Byte
values.
Double Quote
Should be used to designate String
values.
Basic types
int
,uint
(only positives),int32
float64
string
bool
Custom types (alias)
Create alias types with type
keyword.
Add methods to these aliases
Inject methods to these custom type aliases.
Check a value type
You can check if a variable is of a specific type.
You may also do stuff depending on the variable type.
value.(type)
is only available inside this switch
.
Generic types
When you have to accept multiple types, but using interface{}
is just too wide.
Instead of working with interface{}
.
Use a generic type, and specify which types are valid.
Multiple generic types
Fetch user input (Stdin)
fmt.Scan
You can't easily fetch multi-word input values.
bufio.NewReader(os.Stdin)
To read multiple words
Formatting strings
Multiline strings
Variable scopes
Variables or constants can be declared outside of main()
.
In this case a
will be accessable/scoped to the entire file main.go
.
But you can't declare these variables with :=
.
Unamed variables
You may use _
as variables to handle values that will not be used.
Constants
Declare constants with const
.
Pointers
Pointers are variables that store memory addresses instead of values.
Use them to directly mutate variables or to avoid memory duplication.
By default Go pass variables by value to functions.
Pass parameter by reference
Arrays
Declared only
Arrays that are only declared:
With size: Will be "empty", but they will ocupy
4 * int
space in memory.Without size: Will trutly be empty and ocupy no space in memory.
Instantiated (Specified size)
If a size is specified than the Array will have that size, no more and no less.
Instantiated (Dynamic size)
If no size was specifed, a Slice is created.
Access value of array
Accessing indexes out of range will generate a panic with 'runtime error'.
Lenght and Capacity
Lenght gives the number of values in the Slice or Array.
Capacity:
In Array is the same as
len
.In a Slice will be the size of the Array by which the Slice was generated initialy.
Slices
A subset (start, end]
of an Array or other Slices. (Including the start index, and excluding the end index)
Slices are created by REFERENCE. (They are not copies)
You can't use negative indexes.
You can't provide indexes that are out of range.
append
(values)
append
(values)Append only works with Slices.
You may append more than one value at a time.
append
(slices)
append
(slices)You can append/merge entire Slices also, with the ...
operator (Similar to Typescript spread
).
Removing elements
Removing the elements are done by slicing the Slice.
make
function
make
functionmake(slice-type, slice-lenght, underlying-array-capacity)
Use the make
function to initially pre-allocate and optionally fill a specific amount of space in a Slice.
If set the Slice will be filled with the default "null" value of the Slice type.
Ex.: []int
Slice will be filled with zeros.
The pre-allocation to avoid Go to keep recreating bigger underlying arrays is only efficient if slice-length
is 0
.
Otherwise the these Array positions will be occupied with empty values.
Maps
Maps are key: value
pairs of data, and have always dynamic size.
Just like Slices, Go will under the hood, recreate Maps when their memory capacity is reached.
Optimize this with make function.
Are Maps sortable thought??
Declare only
Maps that are only declared will be "empty" and ocupy no memory.
Instanciate
Access value of map
Acessing keys that don't exist will return the "null" value of the value type.
No errors are generated.
Mutating the map
You can add new key: value
pairs by using unexisting keys.
Edit existing key: value
pairs by overwriting its value.
Delete a key
Deleting unexisting keys produces no errors.
make
function
make
functionmake(map-type, size)
Pre-allocate more space in memory with make
.
Last updated