Skip to content

Instantly share code, notes, and snippets.

@martinusso
Created September 25, 2020 19:34
Show Gist options
  • Save martinusso/f012e567f12d110bf634c1cf7e598827 to your computer and use it in GitHub Desktop.
Save martinusso/f012e567f12d110bf634c1cf7e598827 to your computer and use it in GitHub Desktop.
id
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