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

Microphone Test

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):

bash
uuidgen

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:

csharp
using 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:

go
import ( "github.com/google/uuid")func main() { uuid := uuid.NewUUID() fmt.Println(uuid)}

UUID v4:

go
import ( "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:

java
import java.util.UUID;public class Example { public static void main(String[] args) { // Use a library like `com.fasterxml.uuid` for UUID v1 }}

UUID v4:

java
import 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:

javascript
const { v1: uuidv1 } = require('uuid');console.log(uuidv1());

UUID v4:

javascript
const { 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:

kotlin
import com.fasterxml.uuid.Generatorsfun main() { val uuid = Generators.timeBasedGenerator().generate() println(uuid)}

UUID v4:

kotlin
import 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:

php
require 'vendor/autoload.php';use Ramsey\Uuid\Uuid;$uuid = Uuid::uuid1();echo $uuid->toString();

UUID v4:

php
require '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:

python
import uuidprint(uuid.uuid1())

UUID v4:

python
import uuidprint(uuid.uuid4())

UUID v7: There is no native support for UUID v7 in the Python uuid library yet.

Ruby

UUID v1:

ruby
require 'uuidtools'puts UUIDTools::UUID.timestamp_create

UUID v4:

ruby
require 'securerandom'puts SecureRandom.uuid

UUID v7: No direct support in standard libraries.

Rust

UUID v1:

rust
use uuid::Uuid;fn main() { let uuid = Uuid::new_v1(); println!("{}", uuid);}

UUID v4:

rust
use 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:

typescript
import { v1 as uuidv1 } from 'uuid';console.log(uuidv1());

UUID v4:

typescript
import { v4 as uuidv4 } from 'uuid';console.log(uuidv4());

UUID v7: UUID v7 is not supported in common libraries yet.

VB.NET

UUID v1:

vbnet
Imports SystemModule Module1 Sub Main() ' Use a library or manual implementation for UUID v1 End SubEnd Module

UUID v4:

vbnet
Imports 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.

UUID Tools