Getting Started Flashcards

1
Q

How to declare a variable in go and initialize it with a value?

A

var i int = 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can go infer types? How to declare a variable with an inferred type?

A

yes.

name := “Jhon Doe”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to declare a pointer of a string? What is the default value of an empty pointer (pointer that doesn’t point to anything)?

A

var name *string

nil

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is de-referencing in go? How to dereference a string?

A

Is accessing the actual pointer content instead of the address.
*name = “Jhon Doe”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens if I try to dereference a pointer that was not initialized? How to fix?

A

It will throw the invalid memory address or nil pointer dereference error.
var name *string = new(string)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is fmt.Println(name) output? How to output the content?

A

The pointer’s memory address (e.g: 0x123beef)

By dereferencing fmt.Println(*name)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is pointer arithmetic? Does golang support that? Why?

A

Is manipulating the memory location fmt.Println(*(name) + 1). Golang doesn’t support that because it is too error prone.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can I grab the address of a variable in go?

A

ptr := &name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an implicit-typed constant in go?

A

Is a constant value that will adjust itself to match the operation (e.g: const +1 and const + 1.2).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Should I define the value of a const in the same line of the declaration? How does it look like?

A

Yes. const c = 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can I define a type in a constant? What happens then?

A

Yes. The type is checked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is iota? When does it resets?

A

Is an unique-automatic-increment value that can be used in compile time. Resets in a new const() block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Do I have to re-state iota everytime?

A

No. When not specified below an iota, the other consts will get iota automatically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can a const use the return value of a function?

A

no. consts can only be assigned with compile-time stuff - NOT runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What replaces a class in go?

A

struct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an array in golang? How to declare one and set value?

A

A fixed-size and same-type list of elements.
var arr [3] int //arr := [3]int{1,2,3}
arr[0] = 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a slice? Does it have a copy of the array? How to declare one

A

Is the full array or a part of the array. Does not contain a copy of the values (changes the sliced array).

slice = arr[:]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Can I use slice to create a non-fixes size array? How to declare one? How to add a value to the slice?

A

Yes.
slice := []int{1,2,3}
append(slice, 4)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How to get a slice of the array using start and end (BUT NOT INCLUDING) positions?

A

slice2 := slice[1:2] // read the second element only because it reads until 2 but NOT including 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How to declare a hash table that has a key of string and value of int in go?

A

map := map[string]int

21
Q

What is the only heterogeneous type on golang? What does it mean?

A

struct. It can hold different types of data.

22
Q

Can I add fields to a struct in run time?

A

No. Compile time only

23
Q

how to declare a struct?

A
type user struct {
  ID int
  FirstName string
  LasrName string
}
24
Q

How to use the user struct?

A

var u user;

25
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"}
26
Where can I create a struct?
Anywhere: in the function scope, package scope and etc,
27
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.
28
What is a package?
Is the a code grouping (namespace?) mechanism. A module can have many of these.
29
What is the first thing I need to do when creating a go file?
Declaring the package (namespace)
30
What happens if I create a file in a subdirectory?
vscode will suggest it as the package name
31
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 ```
32
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
33
How to use a struct called User from a model called models?
u := models.User{}
34
What happens to the go executable created with the go run command?
It is thrown away after the execution is completed?
35
How to create a persistent executable?
go build
36
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).
37
How to discard a result of a function in go?
_, err := doSomething()
38
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).
39
How to associate related behaviors together in go?
create an empty struct then created the "extension methods"
40
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.
41
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.
42
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.
43
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
44
does break and continue (within a for) statements work on golang?
yes.
45
What does the range statement does?
Returns the index and the value in that index
46
What does the panic statement does?
Halts the application and prints the panic message with some tracing.
47
Is panic state recoverable?
Yes.
48
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
49
How to add a condition to the switch that happens when none matches?
default: