"UUIDs in 10 Languages" Part 2: Rust, Go, and C#

    February 19, 2024
    10 min read
    Tutorial
    Code examples
    uuid
    distributed-systems

    UUIDs in 10 Languages — Part 2: Rust, Go, and C#

    UUIDs are everywhere — and every programming language has its own take on generating, parsing, and working with them.

    In this second part of the “UUIDs in 10 Languages” series, we’ll explore how to use UUIDs effectively in Rust, Go, and C#.


    🦀 UUIDs in Rust

    Rust’s ecosystem provides the uuid crate — fast, full-featured, and RFC-compliant.

    🔧 Installation

    Add this to your Cargo.toml:

    toml
    [dependencies]
    uuid = { version = "1", features = ["v4"] }

    > You can also enable v1, v5, or serde features as needed.

    📦 Generate a UUIDv4 (Random)

    rust
    use uuid::Uuid;
    
    fn main() {
        let id = Uuid::new_v4();
        println!("Generated UUIDv4: {}", id);
    }

    🧪 UUIDv1 (Time-based) — with `getrandom` + `mac_address`

    toml
    [dependencies]
    uuid = { version = "1", features = ["v1"] }
    rust
    use uuid::{Uuid, Timestamp};
    use std::time::{SystemTime, UNIX_EPOCH};
    
    fn main() {
        let ts = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
        let ts = Timestamp::from_unix(&uuid::NoContext, ts.as_secs(), ts.subsec_nanos());
        let id = Uuid::new_v1(ts, &[1, 2, 3, 4, 5, 6]).unwrap();
        println!("UUIDv1: {}", id);
    }

    > For production use, derive the MAC automatically using mac_address crate.

    🧠 Best Practices

    • Use Uuid::new_v4() for general use
    • Use Uuid::new_v5() if you need deterministic identifiers
    • Enable only the features you need to reduce binary size
    • Serialize with serde using uuid's built-in support

    🐹 UUIDs in Go

    Go doesn’t include UUID generation in the standard library — but the community provides excellent packages.

    🔧 Install Google’s UUID Package

    bash
    go get github.com/google/uuid

    📦 Generate UUIDv4

    go
    package main
    
    import (
        "fmt"
        "github.com/google/uuid"
    )
    
    func main() {
        id := uuid.New()
        fmt.Println("UUIDv4:", id.String())
    }

    🧪 UUIDv5 (Namespace + Name)

    go
    package main
    
    import (
        "fmt"
        "github.com/google/uuid"
    )
    
    func main() {
        ns := uuid.MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") // DNS namespace
        id := uuid.NewMD5(ns, []byte("example.com"))
        fmt.Println("UUIDv3:", id)
    
        id5 := uuid.NewSHA1(ns, []byte("example.com"))
        fmt.Println("UUIDv5:", id5)
    }

    🧠 Best Practices

    • Use uuid.New() for UUIDv4 — it’s the Go-to choice (pun intended)
    • Use v3/v5 for deterministic IDs (e.g., from strings or paths)
    • Don’t roll your own UUIDs using math/rand — it’s not secure

    👩‍💻 UUIDs in C#

    C# includes UUIDs (called Guid) in its standard library.

    📦 Generate a GUID

    csharp
    using System;
    
    class Program
    {
        static void Main()
        {
            Guid id = Guid.NewGuid();
            Console.WriteLine("GUID (UUIDv4): " + id.ToString());
        }
    }

    🎯 Parse and Validate

    csharp
    string str = "3f2504e0-4f89-11d3-9a0c-0305e82c3301";
    Guid guid;
    
    if (Guid.TryParse(str, out guid))
    {
        Console.WriteLine("Valid GUID: " + guid);
    }
    else
    {
        Console.WriteLine("Invalid UUID");
    }

    🔎 Format Variants

    csharp
    Guid id = Guid.NewGuid();
    
    Console.WriteLine(id.ToString("D")); // Default: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    Console.WriteLine(id.ToString("N")); // No hyphens
    Console.WriteLine(id.ToString("B")); // Braced
    Console.WriteLine(id.ToString("P")); // Parenthesized

    🧠 Best Practices

    • Use Guid.NewGuid() — it generates a cryptographically strong UUIDv4
    • Avoid exposing Guid in URLs unless formatted safely (use ToString("N"))
    • Consider sorting or indexing implications — GUIDs are not ordered by default

    🧪 Performance and Interop Tips

    LanguageUUID TypeNotes
    Rustuuid::UuidStrong type, supports many formats
    Gouuid.UUIDSimple struct, implements String
    C#System.GuidRich formatting + built-in parsing

    Interop Suggestions

    • Use UUIDv4 across services for maximum compatibility
    • Encode UUIDs in canonical form (lowercase-hyphenated) to avoid mismatches
    • Serialize consistently across languages (e.g., using JSON with string UUIDs)

    🧭 When to Use Each Version

    Use CaseBest UUID Version
    Random ID for API or DBUUIDv4
    Name-based deterministicUUIDv5
    Ordered UUIDsUUIDv7 (not widely supported yet)
    Custom formatsUUIDv8 or alternatives (ULID, KSUID)

    Final Thoughts

    Rust, Go, and C# each provide solid UUID support — whether built-in or via trusted libraries. If you're building distributed systems, APIs, or databases, knowing how to properly use UUIDs in your language of choice is a must.

    Pick the right version, serialize consistently, and embrace UUIDs for what they are: simple, scalable, and uniquely universal.

    Stay tuned for Part 3!

    Generate Your Own UUIDs

    Ready to put this knowledge into practice? Try our UUID generators:

    Generate a Single UUID

    Create a UUID with our fast, secure generator

    Bulk UUID Generator

    Need multiple UUIDs? Generate them in bulk

    Summary

    This article explores UUID generation in Rust, Go, and C#, complete with best practices, code examples, and language-specific implementation tips. Learn how to properly use UUIDs in distributed systems across these three modern ecosystems.

    TLDR;

    Learn how to generate and use UUIDs in Rust, Go, and C# with idiomatic code examples and practical tips.

    Key points:

    • Rust uses the uuid crate, supporting v1, v4, and v5
    • Go relies on Google’s uuid library, great for v4 (random) UUIDs
    • C# includes Guid in the standard library, with easy-to-use constructors

    Whether you need randomness, determinism, or compatibility across services, each language has a solid UUID strategy for you.

    Cookie Consent

    We use cookies to enhance your experience on our website. By accepting, you agree to the use of cookies in accordance with our Privacy Policy.