Error: In stack of struct in go-lang get overwrite? How can we do deep-copy of struct?

Viewed 19

I have created a stack of struct in golang.

type Stack struct {
    stack []StateContextStackCompartment
}

I have this struct and method to create a new struct instance:-

type StateContextStackCompartment struct {
    State StateContextStackState
    StateArgs map[string]interface{}
    StateVars map[string]interface{}
    EnterArgs map[string]interface{}
    ExitArgs map[string]interface{}
    _forwardEvent_ *framelang.FrameEvent
}

func NewStateContextStackCompartment(state StateContextStackState) *StateContextStackCompartment {
    c := &StateContextStackCompartment{State: state}
    c.StateArgs = make(map[string]interface{})
    c.StateVars = make(map[string]interface{})
    c.EnterArgs = make(map[string]interface{})
    c.ExitArgs = make(map[string]interface{})
    return c
}

case:-

I have added value and made changes. m._compartment is the pointer of StateContextStackCompartment

m._compartment_.StateVars["z"] = m._compartment_.StateVars["z"].(int) + 10

Example:-

  1. z value is 30
  2. push the pointer into the stack
  3. z value is changed to 40

Now z value with 40 is not pushed in the stack but the stack got updated with 40 from 30. Deep copy is not happening here. Can you help with what changes I must make before pushing the pointer into the stack?

0 Answers
Related