|
|
|
|
|
by pcwalton
3104 days ago
|
|
Couldn't you do: func closeIfNecessary(object interface{}) {
closable, needsClosing := object.(closable)
if needsClosing {
closable.close()
}
}
And then just do: func f(i interface{}) {
defer closeIfNecessary(i)
...
}
Doing it this way also saves you boilerplate by factoring the downcast out into a separate function. |
|
If you can't call it unless [some other conditions], it goes back to the same kind of problem though. "closable" may not be a good choice on my part, as they're often called unconditionally.