79322549

Date: 2025-01-02 00:20:46
Score: 1
Natty:
Report link

Use the following code to switch on pointer types:

d := &data{}
switch p := ptr.(type) {
case *float64:
    // We handle this in two steps because int(*p)
    // is not addressable. 
    // (1) Convert to int and assign to variable.
    i := int(*p)
    // (2) Assign address of variable to field.
    d.timeout = &i
case *int:
    d.timeout = p
case nil:
    // Clear field when p == nil.
    d.timeout = nil
default:
    panic(fmt.Sprintf("unexpected type %T", p))
}
Reasons:
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Leah Johnston