Creating Custom Data Types with Go Flashcards

1
Q

When is it recommended to use pointer references even on read only methods?

A

When the struct is huge and you don’t want to have multiple copies of the given structure

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

What if I use one reference in a method on a file with multiple methods, what is recommended next?

A

Make all methods to use references to keep consistency.

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

How to declare a string alias? When is it useful?

A

type MyString = string
To facilitate the reading of our code
“Inherits” all fields and methods.

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

What is a type definition declaration? What it defers from an alias? How to declare?

A

Is actually creating a new type from golang’s perspective.
Type definition declaration allows creating extension methods over the new type (while the alias won’t allow). Need casts to the original type (alias were simpler in this case)
type MyString string

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

What is an embedded type? How to declare?

A

Is embedding all fields and methods from a struct into the new struct (without having to access it as struct1.struct2.name.
type Person struct {
Name
}

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

Can I embed interfaces into structs?

A

yes

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

What happens if my struct is embedded with two interfaces that has the same name?

A

it will conflict… you can implement the second interface specific’s signature.

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

Can I compare two instances of the same struct? What happens If I have complex types such as array and func?

A

It is going to compare the contents of the struct normally. Will no longer be able to compare.

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

How does interface equality work? What if it has complex types, will the compiler allow? What about the runtime?

A

Same interface types with the same content are equal.

Compiler allows but runtime it’ll panic.

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

How to create a custom comparer?

A

Create an extension with the Equals keyword (but you still need to call equalls manually)

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

Does go automatically cast our interface type when using inside an switch (if the switch has a type condition, e.g: case string:)? Is it the same for structs?

A

yes. yes

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