
In this tutorial, Learned about how to generate GUID in golang with go.uuid and google.uuid packages.
a unique identifier is a unique string to represent information in software applications.
Two types of unique identifiers are used in applications.
UUID
- universally unique identifier,GUID
- globally unique identifier
These are used in the database for columns to act as the primary key in MongoDB or SQL database. And also you can store cookies or session-id in front-end applications.
UUID
or GUID
is alias both refer same and contains 16 bytes or 128 bits in size separated in 5 groups by hyphens.
You can check my previous posts.
- How to Generate GUID, UUID, UDID in javascript.
- How to generate GUID in java
- React GUID generate
- Angular GUID generate
There are multiple ways we can generate unique identifiers in the Go language
In this example, We are going to use the google/uuid
package to generate uuid
Generate UUID in Go language
Install the package
go get github.com/google/uuid
Here is an example program to generate UUID in the go language
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
uuidValue := uuid.New()
fmt.Println("%s", uuidValue)
}
And the output
ce547c40-acf9-11e6-80f5-76304dec7eb7
Generate UUID in Go language
Install the package
go get github.com/google/uuid
Here is an example program to generate UUID in the go language
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
uuidValue := uuid.New()
fmt.Println("%s", uuidValue)
}
And the output
ce547c40-acf9-11e6-80f5-76304dec7eb7
Generate all versions of UUID in Go Language
go.uuid
package is popular for generate UUID with all v1,v2,v3,v4,v5 versions.
Next, Command line, install a package with the below command
go get github.com/satori/go.uuid
Example program to generate all versions of UUID in golang
package main
import (
"fmt"
"github.com/satori/go.uuid"
)
func main() {
v1value, err := uuid.NewV1()
fmt.Println("%s", myuuid)
v2value, err := uuid.NewV2()
fmt.Println("%s", v2value)
v3value, err := uuid.NewV3()
fmt.Println("%s", v3value)
v4value, err := uuid.NewV4()
fmt.Println("%s", v3value)
v5value, err := uuid.NewV5()
fmt.Println("%s", v4value)
}
Output
8e6a0a6b-895d-4a06-80b4-1f59e3be595e
bade98bd-c2b0-4430-9038-0196c7f16dfc
e2f35db9-11b3-4353-aa7c-1f578bd142e6
b351ec0c-5c93-4474-9c31-ad13b0960b97
e1e2c956-7f42-4e4d-bcea-47ca6ee19e9e