golang UUID库总结

  • github.com/lithammer/shortuuid --> ApXTmAAr3kzdc4amUmbNAE
  • github.com/google/uuid --> 84b3a632-3caa-4eda-a38e-90490df4ed4d
  • github.com/rs/xid --> bp75p1fffgvhc04llp50
  • github.com/segmentio/ksuid --> 1Y4F8CGcQwckA4Iq02jtasTdnmL
  • github.com/kjk/betterguid --> -M0X5LUF2iRIATPVtgZc
  • github.com/oklog/ulid --> 01E1H1JSYGGVQZXPKJG0TY2WXD
  • github.com/sony/sonyflake --> 40528a7fc004b01
  • github.com/chilts/sid --> 1582193797082824800-0206775980367302090
  • github.com/satori/go.uuid --> 0dfaf3b2-4ea6-4066-80dd-71da02da5712
  • github.com/gitstliu/go-id-worker --> 2101641763637788672
package main

import (
	"fmt"
	"log"
	"math/rand"
	"time"

	"github.com/chilts/sid"
	guuid "github.com/google/uuid"
	"github.com/kjk/betterguid"
	"github.com/lithammer/shortuuid"
	"github.com/oklog/ulid"
	"github.com/rs/xid"
	"github.com/satori/go.uuid"
	"github.com/segmentio/ksuid"
	"github.com/sony/sonyflake"

	"github.com/gitstliu/go-id-worker"
)


func genShortUUID() {
	id := shortuuid.New()
	fmt.Printf("github.com/lithammer/shortuuid: %s\n", id)
}



func genUUID() {
	id := guuid.New()
	fmt.Printf("github.com/google/uuid:         %s\n", id.String())
}


func genXid() {
	id := xid.New()
	fmt.Printf("github.com/rs/xid:              %s\n", id.String())
}

func genKsuid() {
	id := ksuid.New()
	fmt.Printf("github.com/segmentio/ksuid:     %s\n", id.String())
}

func genBetterGUID() {
	id := betterguid.New()
	fmt.Printf("github.com/kjk/betterguid:      %s\n", 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\n", id.String())
}

func genSonyflake() {
	flake := sonyflake.NewSonyflake(sonyflake.Settings{})
	id, err := flake.NextID()
	if err != nil {
		log.Fatalf("flake.NextID() failed with %s\n", err)
	}
	// Note: this is base16, could shorten by encoding as base62 string
	fmt.Printf("github.com/sony/sonyflake:      %x\n", id)
}

func genSid() {
	id := sid.Id()
	fmt.Printf("github.com/chilts/sid:          %s\n", id)
}

func genUUIDv4() {
	id, err := uuid.NewV4()
	if err != nil {
		log.Fatalf("uuid.NewV4() failed with %s\n", err)
	}
	fmt.Printf("github.com/satori/go.uuid:      %s\n", id)
}


func idwork(){
	currWoker := &idworker.IdWorker{} 
	currWoker.InitIdWorker(1000, 1)
	newId,_ := currWoker.NextId()
	fmt.Printf("github.com/gitstliu/go-id-worker:      %d\n", newId)
}

func main(){
	genShortUUID()

	genUUID()

	genXid()

	genKsuid()

	genBetterGUID()

	genUlid()

	genSonyflake()

	genSid()

	genUUIDv4()

	idwork()
}

你可能感兴趣的:(golang,实例,golang,uuid,总结)