daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest Oct 12th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. typ := &TYP{InternalState: "filled"}
  2. setNil(typ)
  3.  
  4. fmt.Printf("Expecting that %v to be nil", typ)
  5.    
  6. func setNilIf(v *interface{}) {
  7.     *v = nil
  8. }
  9.    
  10. var i interface{} = "Bob"
  11. fmt.Printf("Before: %vn", i)
  12. setNilIf(&i)
  13. fmt.Printf("After: %vn", i)
  14.    
  15. Before: Bob
  16. After: <nil>
  17.    
  18. func setNilPtr(p unsafe.Pointer) {
  19.     *(**int)(p) = nil
  20. }
  21.    
  22. typ := &TYP{InternalState: "filled"}
  23. fmt.Printf("Before: %vn", typ)
  24. setNilPtr(unsafe.Pointer(&typ))
  25. fmt.Printf("After: %vn", typ)
  26.    
  27. Before: &{filled}
  28. After: <nil>
  29.    
  30. func setNilPtr2(i interface{}) {
  31.     v := reflect.ValueOf(i)
  32.     v.Elem().Set(reflect.Zero(v.Elem().Type()))
  33. }
  34.    
  35. typ2 := &TYP{InternalState: "filled"}
  36. fmt.Printf("Before: %vn", typ2)
  37. setNilPtr2(&typ2)
  38. fmt.Printf("After: %vn", typ2)
  39.    
  40. Before: &{filled}
  41. After: <nil>
  42.    
  43. i = nil
  44. typ = nil
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top