Getting Started Flashcards
How to declare a variable in go and initialize it with a value?
var i int = 10
Can go infer types? How to declare a variable with an inferred type?
yes.
name := “Jhon Doe”
How to declare a pointer of a string? What is the default value of an empty pointer (pointer that doesn’t point to anything)?
var name *string
nil
What is de-referencing in go? How to dereference a string?
Is accessing the actual pointer content instead of the address.
*name = “Jhon Doe”
What happens if I try to dereference a pointer that was not initialized? How to fix?
It will throw the invalid memory address or nil pointer dereference error.
var name *string = new(string)
What is fmt.Println(name) output? How to output the content?
The pointer’s memory address (e.g: 0x123beef)
By dereferencing fmt.Println(*name)
What is pointer arithmetic? Does golang support that? Why?
Is manipulating the memory location fmt.Println(*(name) + 1). Golang doesn’t support that because it is too error prone.
How can I grab the address of a variable in go?
ptr := &name
What is an implicit-typed constant in go?
Is a constant value that will adjust itself to match the operation (e.g: const +1 and const + 1.2).
Should I define the value of a const in the same line of the declaration? How does it look like?
Yes. const c = 3
Can I define a type in a constant? What happens then?
Yes. The type is checked.
What is iota? When does it resets?
Is an unique-automatic-increment value that can be used in compile time. Resets in a new const() block.
Do I have to re-state iota everytime?
No. When not specified below an iota, the other consts will get iota automatically.
Can a const use the return value of a function?
no. consts can only be assigned with compile-time stuff - NOT runtime.
What replaces a class in go?
struct
What is an array in golang? How to declare one and set value?
A fixed-size and same-type list of elements.
var arr [3] int //arr := [3]int{1,2,3}
arr[0] = 1
What is a slice? Does it have a copy of the array? How to declare one
Is the full array or a part of the array. Does not contain a copy of the values (changes the sliced array).
slice = arr[:]
Can I use slice to create a non-fixes size array? How to declare one? How to add a value to the slice?
Yes.
slice := []int{1,2,3}
append(slice, 4)
How to get a slice of the array using start and end (BUT NOT INCLUDING) positions?
slice2 := slice[1:2] // read the second element only because it reads until 2 but NOT including 2
How to declare a hash table that has a key of string and value of int in go?
map := map[string]int
What is the only heterogeneous type on golang? What does it mean?
struct. It can hold different types of data.
Can I add fields to a struct in run time?
No. Compile time only
how to declare a struct?
type user struct { ID int FirstName string LasrName string }
How to use the user struct?
var u user;
What is the so called implicit initialization syntax? What is the symbol used? Example of one.
Means I can create a variable without declaring its type.
:=
user := user{1, “Erikson”, “asd”}
Where can I create a struct?
Anywhere: in the function scope, package scope and etc,
How files are associated with a module?
All the .go files in the same directory or below a go.mod file is part of the same module.
What is a package?
Is the a code grouping (namespace?) mechanism. A module can have many of these.
What is the first thing I need to do when creating a go file?
Declaring the package (namespace)
What happens if I create a file in a subdirectory?
vscode will suggest it as the package name
Can my package have its own (static) variables declared? How? What happens to the implicit initialization syntax?
Yes. var ( user []*User nextID = 1 ) it becomes "=" again
What is a multi-line initialization syntax? What is special about that?
u := models.User { ID: 2, first: "Erikson", last: "asd", // < you need to add this comma here }
need to add a comma at the end of the last line
How to use a struct called User from a model called models?
u := models.User{}
What happens to the go executable created with the go run command?
It is thrown away after the execution is completed?
How to create a persistent executable?
go build
Is it common to raise exceptions with go? How is it done?
No. Most functions when called return the expected result (assigned when works) and the error (assigned when does not work).
How to discard a result of a function in go?
_, err := doSomething()
How to add a method to a struct in go? Is this similar to what concept on .net?
You need to add a function OUTSIDE the struct itself since structs only holds data structures.
The function will have a special start that is func (u User) getFullName() string;
Similar to extension methods in .net (but replace the this keyword by the first parenthesis).
How to associate related behaviors together in go?
create an empty struct then created the “extension methods”
How to create a constructor in go? Is it supported?
create a function that start with the name new.. e.g: func newUser() *User. There’s no official support for constructors, this is just a trick.
What happens if I return a pointer to a locally created struct in go?
go will notice that and will promote that pointer to the heap.
How to implement an interface in go? Do I need a special statement for that?
just create a method that matches the interface’s method signature. No special statement.
What is a mux? What is its full name?
Is a device that selects between several analog or digital input signals and forwards the selected input to a single output line.
Multiplexor
does break and continue (within a for) statements work on golang?
yes.
What does the range statement does?
Returns the index and the value in that index
What does the panic statement does?
Halts the application and prints the panic message with some tracing.
Is panic state recoverable?
Yes.
Do I need to explicitly call break after a condition is hit on a switch block? How to skip the break and go to the next condition as well?
No. The break is implicitly there. Add the fallthrough statement
How to add a condition to the switch that happens when none matches?
default: