如何在golang中为UUID设置自定义日期?

huangapple go评论80阅读模式
英文:

How to set a custom date to UUID in golang?

问题

我尝试在Golang中为UUID设置自定义日期,但无法成功设置。以下是我的代码:

package main

import (
	"fmt"
	"time"

	guuid "github.com/google/uuid"
)

func main() {
	//id := uuid.NewUUID()
	t, _, _ := guuid.GetTime()
	sec, nsec := t.UnixTime()
	timeStamp := time.Unix(sec, nsec)
	fmt.Printf("Your unique id is: %s \n", sec)
	fmt.Printf("The id was generated at: %v \n", timeStamp)
}

请帮助我设置UUID的自定义日期。

英文:

I tried to set a custom date to an UUID in golang, but couldn't able to set it. This is my code:

package main

import (
	"fmt"
	"time"

	guuid "github.com/google/uuid"
)

func main() {
	//id := uuid.NewUUID()
	t, _, _ := guuid.GetTime()
	sec, nsec := t.UnixTime()
	timeStamp := time.Unix(sec, nsec)
	fmt.Printf("Your unique id is: %s \n", sec)
	fmt.Printf("The id was generated at: %v \n", timeStamp)
}

Please help me in setting a custom date to UUID

答案1

得分: 1

目前google/uuid库没有提供这样的功能。而且也不应该提供

但是如果你真的想要实现这个功能,你需要重新实现NewUUID函数。你可以复制源代码,并修改其中使用GetTime的部分。

英文:

There isn't a way to do this via the google/uuid library. And it shouldn't be.

But if you really want to do it, you need to re-implement the NewUUID function. You could copy the code from the source and change the part where it uses the GetTime.

huangapple
  • 本文由 发表于 2022年7月26日 13:47:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/73118237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定