Guest User

Untitled

a guest
Feb 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. )
  6.  
  7. type LibID string
  8.  
  9. type ID struct {
  10. LibID
  11. }
  12.  
  13. func (id *ID) String() string {
  14. return string(id.LibID)
  15. }
  16.  
  17. func NewID() *ID {
  18. return &ID{
  19. "NewID",
  20. }
  21. }
  22.  
  23. type Object struct {
  24. ObjID *ID
  25. }
  26.  
  27. type NewObject struct {
  28. ObjID *ID
  29. }
  30.  
  31. func newObject(obj *Object) NewObject {
  32. result := NewObject{
  33. ObjID: obj.ObjID,
  34. }
  35. // Pointer ObjID is referencing the correct ObjID.
  36. fmt.Println(result.ObjID)
  37.  
  38. return result
  39. }
  40.  
  41. func main() {
  42. objs := []*Object{
  43. {
  44. ObjID: NewID(),
  45. },
  46. }
  47.  
  48. newObjs := make([]NewObject, len(objs))
  49. for i, object := range objs {
  50. newObjs[i] = newObject(object)
  51. fmt.Println(newObjs[0].ObjID)
  52. }
  53.  
  54. // Pointer ObjID is no longer referencing something here.
  55. fmt.Println(newObjs[0].ObjID)
  56. }
Add Comment
Please, Sign In to add comment