Go Flashcards
In Go, every program is part of a…
Package
What does the first line of code in Go program look like?
package main
What lets us import files included in the fmt package?
import “fmt”
What is considered white space?
Blank lines
What is the purpose of leaving white space?
Makes code more readable
What is func main() {
}
?
A function. Any code within the {} is executed.
In Go, executable code belongs to what package?
main
How are statements separated?
Ending a line by hitting enter key or by a semicolon “;”
What is a comment?
Text that is ignored upon execution. Used to explain code and make it more readable.
They can be used to prevent code execution when testing an alternative code, as well.
How are comments denoted?
// single line comments
/* multi line comments */
What are the three basic data types? What do they represent?
bool: boolean values, true/false
Numeric: integer types, floating point values, and complex types
string: string values
How do you declare a variable?
var variablename type = value
variablename := value
:=
When this declaration is used, type of variable is inferred.
It’s not possible to declare a variable using := without assigning a value to it.
What happens when a variable is declared without an initial value?
Values get set to default value of its type.
string = “ “
int = 0
bool = false
Can you declare a variable using “:=“ without assigning a value to it?
No
What is the difference between the var and := ?
var
Can be used inside and outside of functions
Variable declaration and value assignment can be done separately
:=
Can only be used inside functions
Variable declaration and value assignment cannot be done separately (must be done on same line)
Multiple Variable Declaration
It is possible to declare multiple variables in same line.
Only possible to declare one type of variable per line
var a, b, c, d int = 1, 3, 5, 7
var (
a int
b int = 1
c string = “hello
)
If type keyword is not specified, different types of variables can be declared in the same line