I have a function that returns (*sql.Rows, error)
in Go. Under some circumstances, there's nothing to return, but also no error. The choices seems to be:
if (...) {
return nil, nil
}
and then, in the caller:
rows, err := fn()
if err != nil {
return nil, err
}
if rows == nil {
...
} else {
for rows.Next() {
...
}
}
or else returning a special error which I then check for. I think it would be a lot more elegant if I could return a valid Rows instance, but that does nothing but return false when its Next()
method is called, like this:
if (...) {
return EmptyRows(), nil
}
and, in the caller:
rows, err := fn()
if err != nil {
return nil, err
}
for rows.Next() {
...
}
I could do something like:
if (...) {
return db.QueryRows("select * from something where true=false"), nil
}
but that seems pretty goofy. Any recommendations?