Generate UUID in Different Languages
Discover how to generate universally unique identifier (UUIDs) in various programming languages with simple, efficient methods. Perfect for ensuring uniqueness
Generating UUIDs in various versions (1, 4, and 7) across different programming languages involves using libraries specific to each language. Below are the examples for each specified language:
Bash
Bash typically relies on external tools for generating UUIDs.
UUID v4 (random-based):
bashuuidgen
For other versions, you would generally need to use a language like Python or an external library/script.
C#
UUID v1:
using System;using System.Net;using System.Security.Cryptography;public static class UUIDGenerator{ public static Guid GenerateTimeBasedGuid() { byte[] uuid = new byte[16]; DateTime baseDate = new DateTime(1900, 1, 1); TimeSpan span = DateTime.Now - baseDate; byte[] time = BitConverter.GetBytes((long)(span.TotalMilliseconds * 10000L)); byte[] clock = BitConverter.GetBytes((short)Environment.TickCount); byte[] node = new byte[6]; RandomNumberGenerator.Create().GetBytes(node); Array.Copy(time, uuid, 8); Array.Copy(clock, 0, uuid, 8, 2); Array.Copy(node, 0, uuid, 10, 6); return new Guid(uuid); }}
UUID v4:
csharpusing System;public class Example{ public static void Main() { Guid uuid = Guid.NewGuid(); Console.WriteLine(uuid); }}
UUID v7: There is no built-in support for UUID v7 in C# yet. You might need to use a third-party library or manually implement it.
Delphi
Delphi lacks native UUID v1, v4, and v7 generation. However, you can use external libraries like JclSysUtils for v4.
Go
UUID v1:
goimport ( "github.com/google/uuid")func main() { uuid := uuid.NewUUID() fmt.Println(uuid)}
UUID v4:
goimport ( "github.com/google/uuid")func main() { uuid := uuid.New() fmt.Println(uuid)}
UUID v7: Currently, Go libraries like google/uuid
do not support UUID v7. You might need a custom implementation.
Java
UUID v1:
javaimport java.util.UUID;public class Example { public static void main(String[] args) { // Use a library like `com.fasterxml.uuid` for UUID v1 }}
UUID v4:
javaimport java.util.UUID;public class Example { public static void main(String[] args) { UUID uuid = UUID.randomUUID(); System.out.println(uuid); }}
UUID v7: You might need an external library or custom implementation for UUID v7.
JavaScript
UUID v1:
javascriptconst { v1: uuidv1 } = require('uuid');console.log(uuidv1());
UUID v4:
javascriptconst { v4: uuidv4 } = require('uuid');console.log(uuidv4());
UUID v7: As of now, UUID v7 is not available in common libraries like uuid
. You might need a custom implementation.
Kotlin
UUID v1:
kotlinimport com.fasterxml.uuid.Generatorsfun main() { val uuid = Generators.timeBasedGenerator().generate() println(uuid)}
UUID v4:
kotlinimport java.util.UUIDfun main() { val uuid = UUID.randomUUID() println(uuid)}
UUID v7: Currently, there's no built-in support for UUID v7 in Kotlin.
PHP
UUID v1:
phprequire 'vendor/autoload.php';use Ramsey\Uuid\Uuid;$uuid = Uuid::uuid1();echo $uuid->toString();
UUID v4:
phprequire 'vendor/autoload.php';use Ramsey\Uuid\Uuid;$uuid = Uuid::uuid4();echo $uuid->toString();
UUID v7: Ramsey UUID library does not support UUID v7 yet.
Python
UUID v1:
pythonimport uuidprint(uuid.uuid1())
UUID v4:
pythonimport uuidprint(uuid.uuid4())
UUID v7: There is no native support for UUID v7 in the Python uuid
library yet.
Ruby
UUID v1:
rubyrequire 'uuidtools'puts UUIDTools::UUID.timestamp_create
UUID v4:
rubyrequire 'securerandom'puts SecureRandom.uuid
UUID v7: No direct support in standard libraries.
Rust
UUID v1:
rustuse uuid::Uuid;fn main() { let uuid = Uuid::new_v1(); println!("{}", uuid);}
UUID v4:
rustuse uuid::Uuid;fn main() { let uuid = Uuid::new_v4(); println!("{}", uuid);}
UUID v7: UUID v7 is not yet supported in Rust's uuid crate.
TypeScript
UUID v1:
typescriptimport { v1 as uuidv1 } from 'uuid';console.log(uuidv1());
UUID v4:
typescriptimport { v4 as uuidv4 } from 'uuid';console.log(uuidv4());
UUID v7: UUID v7 is not supported in common libraries yet.
VB.NET
UUID v1:
vbnetImports SystemModule Module1 Sub Main() ' Use a library or manual implementation for UUID v1 End SubEnd Module
UUID v4:
vbnetImports SystemModule Module1 Sub Main() Dim uuid As Guid = Guid.NewGuid() Console.WriteLine(uuid) End SubEnd Module
UUID v7: UUID v7 is not supported natively in VB.NET.
For some languages and UUID versions not directly supported, consider using third-party libraries or custom implementations.