Guest User

Untitled

a guest
Jul 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. //Create an alternative done function
  2. newDoneFunc := func (string, bool, time.Time) {
  3. println("Hi!")
  4. }
  5.  
  6. message := pubsub.Message{}
  7.  
  8. //Get a reflectable value of message
  9. messageValue := reflect.ValueOf(message)
  10.  
  11. //Get message's doneFunc field
  12. doneFuncField := messageValue.FieldByName("doneFunc")
  13.  
  14. //Get the address of the field
  15. doneFuncFieldAddress := doneFuncField.UnsafeAddr()
  16.  
  17. //Create a pointer based on the address
  18. doneFuncFieldPointer := unsafe.Pointer(doneFuncFieldAddress)
  19.  
  20. //Create a new, exported field element that points to the original
  21. accessibleDoneFuncField := reflect.NewAt(doneFuncField.Type(), doneFuncFieldPointer).Elem()
  22.  
  23. //Set the field with the alternative doneFunc
  24. accessibleDoneFuncField.Set(reflect.ValueOf(newDoneFunc))
Add Comment
Please, Sign In to add comment