Chapters 7-9

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 2:01 AM on 4/24/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

20 Terms

1
New cards

What is a receiver type

when declaring a method at the struct level you include it to determine what struct the method belongs to

func (p person) String() string{

}

this belongs to the person type struct

2
New cards

When to use a value received vs a pointer receiver

A pointer receiver is used to indicate changes in an object or if you need to handle nil instances

3
New cards

how can you call methods with a pointer receiver on a struct

go will convert it for you can do something like

var p person

p.method() // and this is interpreted as (*p).method()

4
New cards

can you call a pointer receiver on a value type struct

nopers, it is outside the method set. but pointers can use methods with pointer and value receiver

5
New cards

What happens if you call a pointer receiver method with a nil pointer

nothing, it’ll attempt to run the function

6
New cards

What happens if you attempt to call a value receiver method with a null pointer

it’ll panic since there is no backing value

7
New cards

is the pointer receiver a value or pointer

value brug. everythings pass by value

8
New cards

What is a method expression

You can create a function variable from a struct’s method

type Adder struct{

start int

}

func (a *Adder) add(num int) int{

retun a.start + num

}

you can then do this

adder := myAdder{}

f1 := Adder.add

var ret = f1(myAdder, 2) // where the first param is the receiver

9
New cards

Does Go have inheritence

No, the closest you can get is definining a type from a type

e.g. type HighScore Score

but you must cast before trying to use highscore where score can be used. The methods however also dont carry over until converted to the original type

10
New cards

How can you simulate enums in Go?

with the iota keyword. Convention is to define a type and then set the first value in the const block to iota. Iota then increments by 1 starting at 0 for every nth line. Setting a specific value ends that chain, but recalling iota will continue it

11
New cards

Give an example of how iota’s can be used as expressions

type BitField int

const (

f1 BitField = 1 « iota // 1

f2 // 2

f3 // 4

f4 // 8

)

12
New cards

what is an embedded struct

a struct defined within another struct with no name only the type. When definining the outer struct you can initialize the inner. This is essentially composition

13
New cards

How can you access an inner struct

s1.inner.field

14
New cards

how can you define an interface

type name interface {} and lists the methods it must define in order to implement them

15
New cards

can a value receiver method satisify a pointer receiver interface?

no sir. it cannot satisfy the method set since a value type cannot take on a recevier method

16
New cards

What is duck typing

typically used in scripting languages. If you can pass an object to as a parameter to another function and it has the required info to continue with the executuion it can do so. I.e. as long as the object has the necessary tools for the job it’ll do it.

Go has a mix of duck typing and interface usage through polymorphism

17
New cards

can you embed interfaces?

absolutely. io.ReadCloser is a good example of this

18
New cards

Where are interface parameters store in memory

on the stack

19
New cards

how are interfaces implemented in the go runtime

as struct with 2 pointer fields. one for the value and one fore the type of the value. if the type is non nil then the interface is non nil

20
New cards

what is the any type?

it is an alias of the interface {} since this can theoretically take on any value since it is satisifed by all value types