UUID (Universally Distinctive Identifier) is a 128-bit worth used to uniquely establish info in pc techniques. UUIDs are generated utilizing algorithms that assure that every of them is exclusive.
They’re generally utilized in programming languages to uniquely establish objects or information. UUIDs will be represented in numerous codecs, however probably the most generally used is the hyphenated format, which appears like this:
53aa35c8-e659-44b2-882f-f6056e443c99
In Go, there are a lot of packages with which you’ll be able to generate a UUID. A few of the extra common ones are github.com/google/uuid
and github.com/gofrs/uuid
. Each of those are primarily based on RFC 4122 specification and help UUID era in numerous variations. On this article, we offer examples of the right way to generate UUIDs with them within the default method, in addition to utilizing extra superior UUID model choice choices.
To generate a UUID utilizing the github.com/google/uuid
package deal, you need to first obtain and set up it utilizing the go get
Terminal command:
go get github.com/google/uuid
Now, you should utilize this package deal in your code:
package deal principal
import (
"fmt"
"github.com/google/uuid"
)
func principal() {
uuid := uuid.NewString()
fmt.Println(uuid)
}
The uuid.NewString()
is the best operate to generate UUID utilizing the github.com/google/uuid
package deal. It returns a random (Model 4) UUID as a string prepared to make use of or print.
UUIDs are standardized and are available in many variants and variations. If you wish to know all of them, a very good place to begin is to learn the Wikipedia article right here. Totally different variations of UUIDs are primarily based on completely different info:
- Model 1 UUIDs are generated from a time and the MAC tackle.
- Model 2 UUIDs are generated from an identifier (often consumer ID), time, and the MAC tackle.
- Model 3 and 5 UUIDs are generated primarily based on hashing a namespace identifier and identify.
- Model 4 UUIDs are primarily based on a randomly generated quantity.
After operating the applying, you will notice a randomly generated UUID within the output:
8cfb429e-0220-42a9-919c-54d6886d5128
To get and set up the github.com/gofrs/uuid
package deal, you might want to execute the command beneath:
go get github.com/gofrs/uuid
After that, you may generate UUID utilizing this package deal in your app:
package deal principal
import (
"fmt"
"log"
"github.com/gofrs/uuid"
)
func principal() {
uuid, err := uuid.NewV4()
if err != nil {
log.Fatalf("did not generate UUID: %v", err)
}
fmt.Println(uuid)
}
The instance above exhibits the right way to generate model 4 UUID. All you might want to do is use the uuid.NewV4()
operate.
As earlier than, the output of the app is a randomly generated ID:
a40d6058-069e-4a7a-b873-41e3bff827be
Generate a UUID of a selected model
If you’re interested by how one can generate a UUID in a distinct model than the most well-liked Model 4, right here is how you are able to do it utilizing each packages.
With the github.com/google/uuid
package deal
package deal principal
import (
"fmt"
"log"
"github.com/google/uuid"
)
func principal() {
// model 1 uuid
v1, err := uuid.NewUUID()
if err != nil {
log.Deadly("can't generate v1 uuid")
}
fmt.Printf("v1 uuid: %vn", v1)
// model 2 uuid
v2, err := uuid.NewDCEGroup()
if err != nil {
log.Deadly("can't generate v2 uuid")
}
fmt.Printf("v2 uuid: %vn", v2)
// model 3 uuid
v3 := uuid.NewMD5(uuid.NameSpaceURL, []byte("https://instance.com"))
fmt.Printf("v3 uuid: %vn", v3)
// model 4 uuid
v4, err := uuid.NewRandom()
if err != nil {
log.Deadly("can't generate v4 uuid")
}
fmt.Printf("v4 uuid: %vn", v4)
// model 5 uuid
v5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("https://instance.com"))
fmt.Printf("v5 uuid: %vn", v5)
}
With the github.com/gofrs/uuid
package deal
package deal principal
import (
"fmt"
"log"
"github.com/gofrs/uuid"
)
func principal() {
// model 1 uuid
v1, err := uuid.NewV1()
if err != nil {
log.Deadly("can't generate v1 uuid")
}
fmt.Printf("v1 uuid: %vn", v1)
// model 3 uuid
v3 := uuid.NewV3(uuid.NamespaceURL, "https://instance.com")
fmt.Printf("v3 uuid: %vn", v3)
// model 4 uuid
v4, err := uuid.NewV4()
if err != nil {
log.Deadly("can't generate v4 uuid")
}
fmt.Printf("v4 uuid: %vn", v4)
// model 5 uuid
v5 := uuid.NewV5(uuid.NamespaceURL, "https://instance.com")
fmt.Printf("v5 uuid: %vn", v5)
}
Observe that the github.com/gofrs/uuid
package deal doesn’t help UUID era in model 2, however as a substitute helps experimental variations 6 and seven (not current within the instance above).
Which UUID model to make use of?
- For those who merely want a novel random UUID with out going into particulars, use Model 4.
- For those who want a novel random UUID and wish to know which one was generated out of your pc, use Model 1. The IDs in Model 1 are generated primarily based on time and MAC tackle and include each a random and a continuing aspect to establish the pc from which they had been generated.
- Use Model 3 or 5 UUID era when you want reproducible outcomes, i.e. you wish to all the time get the identical ID for a given string. As a result of Model 5 makes use of the SHA-1 hashing algorithm, it’s typically safer and advisable than Model 3 which makes use of MD5.