1/19
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
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
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
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()
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
What happens if you call a pointer receiver method with a nil pointer
nothing, it’ll attempt to run the function
What happens if you attempt to call a value receiver method with a null pointer
it’ll panic since there is no backing value
is the pointer receiver a value or pointer
value brug. everythings pass by value
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
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
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
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
)
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
How can you access an inner struct
s1.inner.field
how can you define an interface
type name interface {} and lists the methods it must define in order to implement them
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
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
can you embed interfaces?
absolutely. io.ReadCloser is a good example of this
Where are interface parameters store in memory
on the stack
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
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