package UUID
NewUUID
func NewUUID() UUID
Creates a new random-based universally unique identifier (UUID).
Returns:
-
UUID: A new UUID generated using random values.
Example:
u := graph.NewUUID()
fmt.Println(u.ToString()) // Outputs a string representation of the UUID
ToUUID
func ToUUID(s string) (UUID, error)
Converts a UUID string representation to a UUID type.
Parameters:
-
s: A UUID string representation (
aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee). It doesn’t matter if the UUID is random-based, time-based or hash-based, as long as it is in the expected format.
Returns:
-
On success, returns an equivalent UUID from the UUID string representation.
-
An error, if the UUID string representation cannot be parsed.
Example:
uuid, err := ToUUID("5e2a0a65-1b33-44fa-8ba2-947f83b16be6")
if err != nil {
// Handle error
}
UUID.ToString
func (u UUID) ToString() string
Convert the UUID into its string representation (e.g. "5e2a0a65-1b33-44fa-8ba2-947f83b16be6").
Returns:
-
A string representation of u.
Examples:
uuid := NewUUID()
fmt.Println(uuid.ToString()) // 5e2a0a65-1b33-44fa-8ba2-947f83b16be6
UUID.MarshalJSON
func (u UUID) MarshalJSON() ([]byte, error)
Encodes the UUID into a JSON representation.
Returns:
-
A JSON representation of the UUID.
-
An error if the UUID cannot be marshaled.
Example:
uuid := UUID.NewUUID()
b, _ := json.Marshal(uuid)
fmt.Println(string(b)) // "5e2a0a65-1b33-44fa-8ba2-947f83b16be6"
UUID.UnmarshalJSON
func (u *UUID) UnmarshalJSON(data []byte) error
Decodes a JSON representation of a UUID into a UUID type.
Parameters:
-
data: A JSON representation of a UUID.
Returns:
-
An error if the UUID cannot be unmarshaled.
Example:
var uuid UUID
data := []byte(`"5e2a0a65-1b33-44fa-8ba2-947f83b16be6"`)
if err := uuid.UnmarshalJSON(data); err != nil {
// Handle error
}
fmt.Println(uuid.ToString()) // 5e2a0a65-1b33-44fa-8ba2-947f83b16be6