Golang - Donovan Flashcards
Logs important stuff highlighted in the book.
Variable Declaration
s := “” => only used in a function, cannot be used at the package level
var s string => initialized to zero value
var s = “” => rarely used, used when declaring multiple vars on same line
var s string = “” => explicity identifies the type of the var, important when its value is different then the zero val
:= is used for declaration
= is used for assignment
when using := at least one var on the left needs to be new or there will be compiler error
For Loop
Basic Form
for initialization; condition; post{
}
Like a While loop
for condition {
}
Like a inifinite loop
for {
}
With a range operator
ass := [1, 2, 3]
for index, val := range arr{
}
Command Line Args
package = os
- os.Args provides all the command line args
- os.Args[0] is the command itself
- os.Args[1:] => gives list of all the args (so escapes the first arg)
for index, val := range os.Args[1:]{
fmt.Println(index, val)
}
Declarations in Go
- there are four types of declarations
1. var
2. const
3. type
4. func
Files in Go
package declaration
imports
package leve declarations
funciton level declarations
Variable