Documentation ¶
Overview ¶
Package guid implements a 64-bit global unique k-sortable ID suitable for use as the primary key of a database where entities may be generated on different hosts and merged later. The uid has 31 bits of a timestamp and 32 bits of a hashed value of a pseudorandom number and the content of the entity.
General usage would look like this, (with field_1 and field_2 being two different aspects of the content, such as Title and Text):
guid, err := guid.NewGuidGenerator(). Content(field_1). Content(field_2). Guid()
The resulting guid implements both sql.Scanner and driver.Valuer interfaces (mapping to a 64-bit integer), so can be used directly in database calls.
Index ¶
- Variables
- func Epoch() time.Time
- type Guid
- func (g Guid) Equal(h Guid) bool
- func (g *Guid) IsNull() bool
- func (g Guid) Less(j Guid) bool
- func (g Guid) MarshalJSON() ([]byte, error)
- func (g Guid) MarshalText() ([]byte, error)
- func (g *Guid) Scan(src interface{}) error
- func (g Guid) String() string
- func (g *Guid) UnmarshalJSON(b []byte) error
- func (g *Guid) UnmarshalText(text []byte) error
- func (g Guid) Value() (driver.Value, error)
- type GuidGenerator
Constants ¶
This section is empty.
Variables ¶
var EpochString = "2022 Jan 1"
var NullGuid = Guid{/* contains filtered or unexported fields */}
Functions ¶
Types ¶
type Guid ¶
type Guid struct {
// contains filtered or unexported fields
}
Guid implements the following interfaces: * database/sql/driver.Valuer * databse/sql.Scanner * encoding.TextMarshaler * encoding.TextUnmarshaler * encoding/json.Marshaler * encoding/json.Unmarshaler
The Scanner / Valuer interpret it as an int64, allowing it to be used as a primary key.
func (Guid) MarshalJSON ¶
func (Guid) MarshalText ¶
func (*Guid) UnmarshalJSON ¶
func (*Guid) UnmarshalText ¶
type GuidGenerator ¶
type GuidGenerator struct {
// contains filtered or unexported fields
}
GuidGenerator is used to store information necessary to genreate a Guid. This should always be created using GuidGenerator; then all content added using the Content() methods; and then finally the Guid() method called to get the resulting compact instance of the guid.
func NewGuidGenerator ¶
func NewGuidGenerator() *GuidGenerator
func (*GuidGenerator) Content ¶
func (g *GuidGenerator) Content(e any) *GuidGenerator
Content hashes in the content to the hash portion of the Guid. Internally "encoding/gob" is used to translate objects into bytestreams, so any content must be accessible to that package. Additionally, a Guid type may be passed in.
If an error has already occured in the generation pipeline, this will be ignored. If an error occurs during the encodring, the error will be set, Errors can be read by calling the Guid() method.
If the GuidGenerator has already been evaluated (by calling the Guid() method), this will cause the error to be set.
func (*GuidGenerator) Guid ¶
func (g *GuidGenerator) Guid() (Guid, error)
Guid evaluates the content of GuidGenerator into a Guid type. The result is cached, so that repeated calls to Guid will not do a complete recalculation.
If any errors have happened during the generation pipeline, NullGuid will be returned, along with the error.
Once a GuidGenerator is evaluated with this method, no further content can be added; doing so will cause the error to be set.