{
  "slug": "Resolving-sql--Scan-error-When-Handling-NULL-Strings-in-Go-and-PostgreSQL-762bdf5a66b2",
  "title": "🐞 Resolving sql: Scan error When Handling NULL Strings in Go and PostgreSQL",
  "subtitle": "In Go applications using PostgreSQL, it’s common to map query results into struct fields using the Scan method. However, when database…",
  "excerpt": "In Go applications using PostgreSQL, it’s common to map query results into struct fields using the Scan method. However, when database…",
  "date": "2025-08-06",
  "tags": [
    "Go",
    "Postgres"
  ],
  "readingTime": "1 min",
  "url": "https://medium.com/@mobinshaterian/resolving-sql-scan-error-when-handling-null-strings-in-go-and-postgresql-762bdf5a66b2",
  "hero": "https://cdn-images-1.medium.com/max/800/1*SCDT-ktPArw_4Iw-MpkvRw.jpeg",
  "content": [
    {
      "type": "paragraph",
      "html": "In Go applications using PostgreSQL, it’s common to map query results into struct fields using the <code>Scan</code> method. However, when database columns contain <code>NULL</code> values, scanning directly into native Go types—like <code>string</code>—can result in runtime errors. One such frequent error occurs when scanning a <code>NULL</code> into a <code>string</code> field:"
    },
    {
      "type": "quote",
      "html": "<code><em>sql: Scan error on column index 24, name \"code\": converting NULL to string is unsupported</em></code>"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*SCDT-ktPArw_4Iw-MpkvRw.jpeg",
      "alt": "🐞 Resolving sql: Scan error When Handling NULL Strings in Go and PostgreSQL",
      "caption": "",
      "width": 1024,
      "height": 1024
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Root Cause"
    },
    {
      "type": "paragraph",
      "html": "Go’s <code>database/sql</code> package doesn't allow direct mapping of <code>NULL</code> database values into primitive types. In this case, the <code>discount_code</code> column contains <code>NULL</code>, but the corresponding Go struct expects a plain <code>string</code>, which can't represent <code>NULL</code>. This mismatch triggers the scan error."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "The Solution: sql.NullString"
    },
    {
      "type": "paragraph",
      "html": "Go offers the <code>sql.NullString</code> type—a wrapper that safely handles nullable string fields. Here's how it works:"
    },
    {
      "type": "code",
      "lang": "javascript",
      "code": "var code sql.NullString"
    },
    {
      "type": "paragraph",
      "html": "After scanning:"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "if code.Valid {    b.code = code.String}"
    },
    {
      "type": "paragraph",
      "html": "This approach lets you check whether the value is present (<code>Valid == true</code>) before using it."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Refactored Scan Implementation"
    },
    {
      "type": "paragraph",
      "html": "Below is a corrected version of the <code>scan</code> function:"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "func (s Storage) scan(scanner db.Scanner) (*Book, error) {\n    var b Booking\n    var discountCode sql.NullString\n    err := scanner.Scan(&b.ID, &discountCode,\n    )\n    if err != nil {\n        return nil, serr.DB(\"scan\", \"books\", err)\n    }\n    if discountCode.Valid {\n        b.DiscountCode = discountCode.String\n    }\n    return &b, nil\n}"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Takeaways"
    },
    {
      "type": "paragraph",
      "html": "✅ <strong>Use </strong><code>sql.Null*</code><strong> types</strong> whenever your database may contain <code>NULL</code> values."
    },
    {
      "type": "paragraph",
      "html": "✅ <strong>Guard assignments</strong> to primitive types with&nbsp;<code>.Valid</code> checks to avoid panics."
    },
    {
      "type": "paragraph",
      "html": "✅ <strong>Encapsulate conversions</strong> to clean up your logic — consider writing helpers for mapping <code>sql.NullString</code> to <code>string</code>."
    }
  ]
}
