I’ve the playground example as:
package main
import "fmt"
type Service struct {
name string
namespace string
endpoints []string
mappings map[string]string
}
func main() {
svc := Service{
name: "nginx",
namespace: "default",
endpoints: []string{
"192.168.0.1",
"192.168.0.2",
},
mappings: map[string]string{
"pod-0": "192.168.0.1",
"pod-1": "192.168.0.2",
},
}
nginx := svc
nginx.namespace = "ingress"
nginx.endpoints = append(nginx.endpoints, "192.168.0.40")
nginx.mappings["pod-2"] = "192.168.0.40"
fmt.Printf("updated: %+v\noriginal: %+v\n", nginx, svc)
}
Where I’m having a copy of svc
that is nginx
but once I modified the mapping
part of this copy, I noticed the mapping
in the original item svc
had been changed!
I guess the reason is that nginx
copying the address
of the mapping
used at svc
not the value
I need to:
- Understand how and why this us happening
- How to avoid this, and ensure changing the copy/clone is not going to impact the origin