Created
October 10, 2021 08:40
-
-
Save bopbi/bff51c5b2be3a32784e9344ef790a3ca to your computer and use it in GitHub Desktop.
NullString for MySQL datatype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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