Dear All,
This is my first post here, so please be gentle. I am a new Go coder, having been a C++ coder about 20 years ago, and conducted another career since then. I have only been coding Go for about 1.5 months, and only part time, so I have not seen EVERY tutorial out there. Google sends me to pages that are a bit shallow for this question (which is STILL just a NOOB question, sorry!), so I thought I needed to ask real people.
So, I have two questions about the code which follows. This code is a (much) simplified version of my actual code, but, it has the same bad behaviour I do not understand/want.
-
First question (less important): How come I cannot assign my pointers directly to addresses of objects returned by functions? E.g., lines 44-47 of my code below COULD be
dss.dOnep := &NewDude(“Albert”)
dss.dTwop := &NewDude(“Bella”)
but that does not seem to work (while my given code DOES work).
- More importantly, how come in the final code, the Inversion does not “stick”? That is, I thought I was working directly on the memory of `myDudes’ from line 65 in the call to Invert on line 67, but we can see that while the right thing is happening inside the Invert() function call (see my playground output), on return the order switches back.
Here is the Code:
================================
package main
import (
“fmt”
)
type dudeStruct struct {
dude string
}
// Dude is an interface for the dudeStruct.
// a Dude can be created and gives a HelloString().
type Dude interface {
HelloString() string
}
// NewDude makes a pointer to a new Dude, with given name.
func NewDude(name string) Dude {
var ds dudeStruct
ds.dude = name
return ds
}
// String gives a Dude printString for a dudeStruct object.
func (ms dudeStruct) String() string {
return "Hello from " + ms.dude + ". "
}
// Now a collection of two dudes
type dudesStruct struct {
dOnep *Dude
dTwop *Dude
}
// Dudes is an interface to two dudes as an ordered set.
type Dudes interface {
String() string
Invert()
}
// MakeDudes does what it says. Returns pointer to the Dudes object created.
func MakeDudes(dOne, dTwo string) Dudes {
var dss dudesStruct
albert := NewDude(“Albert”)
bella := NewDude(“Bella”)
dss.dOnep = &albert
dss.dTwop = &bella
return &dss
}
// String gives a Dudes printstring for a dudesStruct object.
func (dss dudesStruct) String() string {
return ((*dss.dOnep).HelloString() + (*dss.dTwop).HelloString())
}
// Invert SHOULD switch the order of the dudes. It does internally, but not after-the-fact.
func (dss dudesStruct) Invert() {
fmt.Println("In Invert() – before inversion: " + dss.String() + "'") swapSpacep := dss.dOnep dss.dOnep = dss.dTwop dss.dTwop = swapSpacep fmt.Println("In Invert() -- after inversion:
" + dss.String() + “’”)
}
func main() {
var myDudes Dudes
myDudes = MakeDudes(“Alice”, “Bob”)
fmt.Println(myDudes.String())
myDudes.Invert()
fmt.Println(myDudes.String())
}
============================
Here is the output from Go Playground:
Hello from Albert. Hello from Bella.
In Invert() – before inversion: `Hello from Albert. Hello from Bella. ’
In Invert() – after inversion: `Hello from Bella. Hello from Albert. ’
Hello from Albert. Hello from Bella.
Clearly, as is right, I want Bella first!!
Also, any (gentle) tips on anything I am doing stupidly would be greatly appreciated.