Skip to content

Commit 706f4ce

Browse files
authored
Add a trick to handle null sql query results
1 parent 1ec3d97 commit 706f4ce

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

nulls.md

+16
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ for rows.Next() {
5353
}
5454
</pre>
5555

56+
Another trick to handle NULLs without using nullable types.
57+
58+
Use pointers.
59+
60+
<pre class="prettyprint lang-go">
61+
for rows.Next() {
62+
var s *string
63+
err := rows.Scan(&amp;s)
64+
// check err
65+
if s != nil {
66+
// use s.String
67+
} else {
68+
// NULL value
69+
}
70+
}
71+
</pre>
5672

5773
**Previous: [Handling Errors](errors.html)**
5874
**Next: [Working with Unknown Columns](varcols.html)**

0 commit comments

Comments
 (0)