I am new to go and currently I am at the point where I just learned that hard-copy values are evil (in some cases). Now I have a small example here and want to to know:
Why do I have to * (dereference) at the marked position in code
Is this efficient what I am trying to achieve there or is there a better option to achieve the same result (with less memory consumption)?
type SomeObject struct {
a string
b string
c string
}
type SomeInterface interface {
setup(name string, unknownAmountOfObjects ...*SomeObject)
}
type InterfaceTest struct {
ExaxtAmountOfObjects *[3]SomeObject
name string
}
// implement the interface by implementing this method
func (interfacetest *InterfaceTest) setup(name string, somename ...*SomeObject) {
interfacetest.name = name
for i, value := range rotors {
// check for size first
if i < len(interfacetest.ExaxtAmountOfObjects) {
// !!!! Why do I need to dereference here??!
interfacetest.ExaxtAmountOfObjects[i] = *value
}
}
}
Why do I have to * (dereference) at the marked position in code
ExaxtAmountOfObjects *[3]SomeObject holds a value of SomeObject but you are passing to function pointer(*SomeObject) to the object, that is why you need to dereference it.
Is this efficient what I am trying to achieve there or is there a better option to achieve the same result (with less memory consumption)?
ExaxtAmountOfObjects is an array which means it is a fixed size. setup is a variadic function, you can pass as much as you want. if I would see your function without it’s implementation I would pass 4 items to the function and expect all the elements in the array.
iterates of rotors which is not mentioned anywhere else in this code snippet. So we have no idea what valueis. It is not releated to somename in the signautre of SomeInterface.setup(...) . What is rotors?