Created
September 25, 2020 19:34
-
-
Save martinusso/f012e567f12d110bf634c1cf7e598827 to your computer and use it in GitHub Desktop.
id
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
package main | |
import ( | |
"database/sql/driver" | |
"github.com/google/uuid" | |
) | |
type ID string | |
func NewID() ID { | |
return ID(uuid.New().String()) | |
} | |
func (id ID) Empty() bool { | |
return id == "" | |
} | |
func (id ID) String() string { | |
return string(id) | |
} | |
// Scan implements the database/sql Scanner interface. | |
func (id *ID) Scan(src interface{}) error { | |
if src == nil { | |
*id = "" | |
return nil | |
} | |
*id = ID(src.(string)) | |
return nil | |
} | |
// Value implements the database/sql/driver Valuer interface. | |
func (id ID) Value() (driver.Value, error) { | |
if id == "" { | |
return nil, nil | |
} | |
return id.String(), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment