I don't seem to understand how pointers work.
My function getPodByName returns a pointer to a pod struct. By modifying this pod, I want to modify the global podList variable PL on which I run getPodByName to recieve the pointer.
My code below has comments that show desired output vs the output that I get.
Any help would be much appreciated!
package main
import (
"fmt"
"time"
)
var PL podList
type pod struct {
name string
nodePort string
state string
}
func NewPodStruct() *pod {
return &pod{
name: "UNAMED",
state: "UNEXPOSED",
nodePort: "EMPTY",
}
}
type podList struct {
podArr []pod
namespace string
pod_name_stem string
statefulset_name string
num_spawned int
}
func (pl *podList) getPodByName(podNameFind string) *pod {
var returnPod *pod
for _, pod := range pl.podArr {
if podNameFind == pod.name {
returnPod = &pod
}
}
return returnPod
}
func main() {
newPod := NewPodStruct()
newPod.name = "abc"
PL.podArr = append(PL.podArr, *newPod)
po := PL.getPodByName("abc")
fmt.Printf("po: %v\n", po)
// desired: po: &{abc EMPTY UNEXPOSED}
// get: po: &{abc EMPTY UNEXPOSED}
po.nodePort = "30777"
po = PL.getPodByName("abc")
fmt.Printf("po: %v\n", po)
// desired: po: &{abc EMPTY 30777}
// get: po: &{abc EMPTY UNEXPOSED}
}