Bat123.com

UUID在线生成器

UUID(Universally Unique Identifier)全局唯一标识符,定义为一个字符串主键,采用32位数字组成,编码采用16进制,定义了在时间和空间都完全唯一的系统信息

生成UUID主流编程语言示例

JavaScript

1// 使用uuid库 2const { v4: uuidv4 } = require('uuid'); 3console.log(uuidv4()); 4 5// 或者使用内置函数(适用于现代浏览器) 6function generateUUID() { 7 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { 8 var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); 9 return v.toString(16); 10 }); 11} 12console.log(generateUUID());

Go

1package main 2 3import ( 4 "fmt" 5 "github.com/google/uuid" 6) 7 8func main() { 9 id := uuid.New() 10 fmt.Println(id.String()) 11}

PHP

1function generateUUID() { 2 return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 3 mt_rand(0, 0xffff), mt_rand(0, 0xffff), 4 mt_rand(0, 0xffff), 5 mt_rand(0, 0x0fff) | 0x4000, 6 mt_rand(0, 0x3fff) | 0x8000, 7 mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) 8 ); 9} 10 11echo generateUUID();

Python

1import uuid 2 3print(str(uuid.uuid4()))

MySQL

在MySQL中,你不能直接生成UUID,但可以使用UUID()或UUID_SHORT()函数来获取一个唯一的标识符。这些函数返回的是二进制格式的UUID,如果你想得到字符串格式的UUID,你需要自己转换。

1SELECT UUID(); -- 返回一个16字节的UUID值 2SELECT HEX(UUID()); -- 将UUID转换为十六进制字符串

Swift

1import Foundation 2 3let uuid = UUID().uuidString 4print(uuid)

Java

1import java.util.UUID; 2 3public class Main { 4 public static void main(String[] args) { 5 UUID id = UUID.randomUUID(); 6 System.out.println(id.toString()); 7 } 8}

C

C标准库也没有提供生成UUID的功能,但你可以使用第三方库如libuuid。

1#include <uuid/uuid.h> 2 3int main() { 4 uuid_t out; 5 uuid_generate(out); 6 char uu[37]; 7 uuid_unparse_lower(out, uu); 8 printf("%s\n", uu); 9 return 0; 10}

C++

C++标准库本身没有提供生成UUID的函数,但你可以使用第三方库如boost或cpprestsdk。

1#include <boost/uuid/uuid.hpp> 2#include <boost/uuid/uuid_generators.hpp> 3#include <boost/uuid/uuid_io.hpp> 4 5int main() { 6 boost::uuids::random_generator gen; 7 boost::uuids::uuid uuid = gen(); 8 std::cout << uuid << std::endl; 9 return 0; 10}

C#

1using System; 2 3class Program { 4 static void Main(string[] args) { 5 Guid guid = Guid.NewGuid(); 6 Console.WriteLine(guid.ToString()); 7 } 8}

Rust

1use uuid::Uuid; 2 3fn main() { 4 let uuid = Uuid::new_v4(); 5 println!("{}", uuid.to_hyphenated().to_string()); 6}

Objective-C

1#import <Foundation/Foundation.h> 2 3int main(int argc, const char * argv[]) { 4 @autoreleasepool { 5 NSUUID *uuid = [[NSUUID alloc] init]; 6 NSLog(@"%@", uuid.UUIDString); 7 } 8 return 0; 9}

Ruby

1require 'securerandom' 2 3puts SecureRandom.uuid

Shell

1# 使用uuidgen命令(可能需要安装uuid-runtime包) 2uuidgen

Lua

Lua本身不提供生成UUID的功能,但是你可以通过一些外部库实现。例如,使用lua-resty-uuid库:

1local uuid = require "resty.jit-uuid" 2local str = uuid.generate_v4() 3ngx.say(str)

Dart

1import 'package:uuid/uuid.dart'; 2 3void main() { 4 final uuid = Uuid().v4(); 5 print(uuid); 6}

SQLite

SQLite不支持生成UUID,但可以通过SQL查询创建自定义函数来生成UUID。以下是一个例子:

1CREATE TABLE TestTable (ID TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))) NOT NULL); 2INSERT INTO TestTable DEFAULT VALUES; 3SELECT ID FROM TestTable;

Erlang

1-module(uuid). 2-export([generate/0]). 3 4generate() -> 5 lists:flatten(io_lib:format("~s-~s-~s-~s-~s", [ 6 uuid1(), 7 uuid2(), 8 uuid3(), 9 uuid4(), 10 uuid5() 11 ])). 12 13uuid1() -> 14 erlang:system_info(system_version). 15 16uuid2() -> 17 integer_to_list(erlang:system_time(microsecond), 16). 18 19uuid3() -> 20 integer_to_list(erlang:unique_integer([positive]), 16). 21 22uuid4() -> 23 integer_to_list(erlang:unique_integer([positive]), 16). 24 25uuid5() -> 26 integer_to_list(erlang:unique_integer([positive]), 16).
© 2024 Bat123.com All Right Reserved.浙ICP备12037580号