From 706f4ceac923bb386c31e6c8596c775441f7a863 Mon Sep 17 00:00:00 2001 From: Yuankui Li Date: Tue, 5 Jun 2018 10:23:00 +0800 Subject: [PATCH] Add a trick to handle null sql query results --- nulls.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/nulls.md b/nulls.md index 274b450..7f24082 100644 --- a/nulls.md +++ b/nulls.md @@ -53,6 +53,22 @@ for rows.Next() { } +Another trick to handle NULLs without using nullable types. + +Use pointers. + +
+for rows.Next() {
+	var s *string
+	err := rows.Scan(&s)
+	// check err
+	if s != nil {
+	   // use s.String
+	} else {
+	   // NULL value
+	}
+}
+
**Previous: [Handling Errors](errors.html)** **Next: [Working with Unknown Columns](varcols.html)**