Cleanest way to check if record in database exist, without caring about its contents (scan into nothing)?

143 Views Asked by At

I have a piece of code that checks whether or not the requested key exists in the database. Its contents are therefore not important in that particular place.

The best way I know how to check for it is to try to Scan() it and see if it returned an error, which leaves the declared and initialized variable which it scanned into unused. My question is, is this the cleanest and most minimal way to solve that problem?

Relevant code (using database/sql from the standard library):

var content string
err := db.QueryRowContext(
    ctx,
    querySelect,
    hashKey,
).Scan(&content)

return err == nil

Scanning into io.Discard doesn't work here as it reports "destination not a pointer".

0

There are 0 best solutions below