Skip to content

Instantly share code, notes, and snippets.

@bopbi
Created October 10, 2021 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bopbi/bff51c5b2be3a32784e9344ef790a3ca to your computer and use it in GitHub Desktop.
Save bopbi/bff51c5b2be3a32784e9344ef790a3ca to your computer and use it in GitHub Desktop.
NullString for MySQL datatype
import (
"database/sql/driver"
"fmt"
)
type NullString string
func (str *NullString) Scan(value interface{}) error {
if value == nil {
*str = ""
return nil
}
strVal := fmt.Sprintf("%s", value)
*str = NullString(strVal)
return nil
}
func (str NullString) Value() (driver.Value, error) {
if len(str) == 0 { // if nil or empty string
return nil, nil
}
return string(str), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment