zoukankan      html  css  js  c++  java
  • go多种uuid生成方式

    package main
    
    import (
    	"fmt"
    	"github.com/chilts/sid"
    	"github.com/kjk/betterguid"
    	"github.com/oklog/ulid"
    	"github.com/rs/xid"
    	"github.com/satori/go.uuid"
    	"github.com/segmentio/ksuid"
    	"github.com/sony/sonyflake"
    	"log"
    	"math/rand"
    	"time"
    )
    
    func genXid() {
    	id := xid.New()
    	fmt.Printf("github.com/rs/xid:           %s
    ", id.String())
    }
    
    func genKsuid() {
    	id := ksuid.New()
    	fmt.Printf("github.com/segmentio/ksuid:  %s
    ", id.String())
    }
    
    func genBetterGUID() {
    	id := betterguid.New()
    	fmt.Printf("github.com/kjk/betterguid:   %s
    ", id)
    }
    
    func genUlid() {
    	t := time.Now().UTC()
    	entropy := rand.New(rand.NewSource(t.UnixNano()))
    	id := ulid.MustNew(ulid.Timestamp(t), entropy)
    	fmt.Printf("github.com/oklog/ulid:       %s
    ", id.String())
    }
    
    func genSonyflake() {
    	flake := sonyflake.NewSonyflake(sonyflake.Settings{})
    	id, err := flake.NextID()
    	if err != nil {
    		log.Fatalf("flake.NextID() failed with %s
    ", err)
    	}
    	fmt.Printf("github.com/sony/sonyflake:   %x
    ", id)
    }
    
    func genSid() {
    	id := sid.Id()
    	fmt.Printf("github.com/chilts/sid:       %s
    ", id)
    }
    
    func genUUIDv4() {
    	id,err := uuid.NewV4()
    	if err != nil {
    		fmt.Printf("get uuid error [%s]",err)
    	}
    	fmt.Printf("github.com/satori/go.uuid:   %s
    ", id)
    }
    
    func main() {
    	genXid()
    	genKsuid()
    	genBetterGUID()
    	genUlid()
    	genSonyflake()
    	genSid()
    	genUUIDv4()
    }
    
  • 相关阅读:
    C# 文件类的操作---删除
    C#实现Zip压缩解压实例
    UVALIVE 2431 Binary Stirling Numbers
    UVA 10570 meeting with aliens
    UVA 306 Cipher
    UVA 10994 Simple Addition
    UVA 696 How Many Knights
    UVA 10205 Stack 'em Up
    UVA 11125 Arrange Some Marbles
    UVA 10912 Simple Minded Hashing
  • 原文地址:https://www.cnblogs.com/dwxt/p/12889653.html
Copyright © 2011-2022 走看看