Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- )
- type LibID string
- type ID struct {
- LibID
- }
- func (id *ID) String() string {
- return string(id.LibID)
- }
- func NewID() *ID {
- return &ID{
- "NewID",
- }
- }
- type Object struct {
- ObjID *ID
- }
- type NewObject struct {
- ObjID *ID
- }
- func newObject(obj *Object) NewObject {
- result := NewObject{
- ObjID: obj.ObjID,
- }
- // Pointer ObjID is referencing the correct ObjID.
- fmt.Println(result.ObjID)
- return result
- }
- func main() {
- objs := []*Object{
- {
- ObjID: NewID(),
- },
- }
- newObjs := make([]NewObject, len(objs))
- for i, object := range objs {
- newObjs[i] = newObject(object)
- fmt.Println(newObjs[0].ObjID)
- }
- // Pointer ObjID is no longer referencing something here.
- fmt.Println(newObjs[0].ObjID)
- }
Add Comment
Please, Sign In to add comment