How to generate a UUID in Go
Go is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. It is often referred to as Golang because of its former domain name, golang.org, but its proper name is Go. Go is syntactically similar to C but has several features that make it more modern and easier to use. Some of these features include memory safety, garbage collection, structural typing, and CSP-style concurrency. Go was designed to improve programming productivity in an era of multicore, networked machines and large codebases. It aims to be efficient, readable, and usable.
This is how you generate a UUID in Go:
- package main
- import (
- "fmt"
- "github.com/google/uuid"
- )
- func main() {
- id := uuid.New()
- fmt.Println(id.String())
- }
This program uses the google/uuid package to generate a version 4 UUID. You can install this package by running go get github.com/google/uuid
Explanation
- Line #1 specifies that the code is part of the main package. The main package is a special package in Go that tells the Go compiler to create an executable binary.
- Lines #2-#5 is a block of code which imports two packages: fmt and github.com/google/uuid. The fmt package provides functions for formatting and printing output, while the google/uuid package provides functions for generating UUIDs.
- Line #6 defines the main function. The main function is the entry point of the program and is where the execution of the program begins.
- Line #7 creates a new variable called id and assigns it a new UUID generated by calling the New function from the google/uuid package.
- Line #8 prints the string representation of the UUID to the standard output using the Println function from the fmt package. The String method is called on the id variable to get its string representation.
How to convert from a string to UUID in Go
This program uses the Parse function from the google/uuid package to convert a string representation of a UUID into a UUID value. The Parse function returns an error if the input string is not a valid UUID.
- package main
- import (
- "fmt"
- "github.com/google/uuid"
- )
- func main() {
- uuidStr := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
- id, err := uuid.Parse(uuidStr)
- if err != nil {
- fmt.Println(err)
- return
- }
- fmt.Println(id)
- }
Explanation
- Line #1 specifies that the code is part of the main package. The main package is a special package in Go that tells the Go compiler to create an executable binary.
- Lines #2-#5 is a block of code imports two packages: fmt and github.com/google/uuid. The fmt package provides functions for formatting and printing output, while the google/uuid package provides functions for working with UUIDs.
- Lines #6 defines the main function. The main function is the entry point of the program and is where the execution of the program begins.
- Line #7 creates a new variable called uuidStr and assigns it a string value that represents a UUID.
- Lines #8 calls the Parse function from the google/uuid package to convert the string representation of a UUID into a UUID value. The Parse function returns two values: a UUID value and an error value. If the input string is not a valid UUID, the error value will be non-nil.
- Lines #9-#11 is a block of code checks if the error value returned by the Parse function is non-nil. If it is non-nil, it means that an error occurred while parsing the UUID string. In this case, the error message is printed to the standard output using the Println function from the fmt package and the program exits.
- Line #12 prints the UUID value to the standard output using the Println function from the fmt package.