Skip to content

How to Validate UUID in GoLang ?

ValidateUUID validates whether the given UUID string is valid.

The function takes a UUID string as input and attempts to parse it using the uuid.Parse() function. If the parsing is successful and the resulting UUID object is not the zero value, the function returns true, indicating that the UUID is valid. Otherwise, it returns false.

Args:

  • uuid (string): The UUID string to be validated.

Returns:

  • bool: True if the UUID is valid, false otherwise.
import (
	"github.com/google/uuid"
)

func ValidateUUID(input string) bool {
	// Parse the input string as a UUID
	uuidObj, err := uuid.Parse(input)

	// Check if the parsing was successful and if the resulting UUID object is
	// not the zero value
	return err == nil && uuidObj != (uuid.UUID{})
}

Experience the magic of coding unfold as you execute this code!

package main

import (
	"fmt"

	"github.com/google/uuid"
)

func ValidateUUID(input string) bool {
	uuidObj, err := uuid.Parse(input)
	return err == nil && uuidObj != (uuid.UUID{})
}

func main() {
	fmt.Println(ValidateUUID("0124e053-3580-7000-b00e-497f2c51d52b")) // true
	fmt.Println(ValidateUUID("1")) // false
	fmt.Println("Build with love by Adam Nasrudin")
}

enjoy your code!