在Golang中,UUID4的整数表示方法是什么?

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

Integer prepresentation for UUID4 in Golang

问题

我正在尝试在Golang中解析UUID4,但我需要一些特定的信息:整数表示。

在Python中,我使用:

  1. uuid.uuid4().int

但在Golang中它不存在(或在我搜索的任何其他uuid库中都不存在)。

有没有一种方法可以将简单的UUID解析为其整数表示形式?

英文:

I'm trying to parse a UUID4 in Golang, but I need some specific info: The Integer representation.

In python I use:

  1. uuid.uuid4().int

but in Golang it doesn't exist (or in any other uuid library that I googled).

Is there a way to parse a simple UUID into its integer representation?.

答案1

得分: 11

假设您的uuid是一个字符串:

  1. func main() {
  2. uuid := `25f64dba-634d-4613-9516-9ca61b161454`
  3. var i big.Int
  4. i.SetString(strings.Replace(uuid, "-", "", 4), 16)
  5. //或者如果您的uuid是[16]byte类型
  6. //i.SetBytes(uuid[:])
  7. fmt.Println(i.String())
  8. }

playground

英文:

Assuming your uuid type a string:

  1. func main() {
  2. uuid := `25f64dba-634d-4613-9516-9ca61b161454`
  3. var i big.Int
  4. i.SetString(strings.Replace(uuid, "-", "", 4), 16)
  5. //or if your uuid is [16]byte
  6. //i.SetBytes(uuid[:])
  7. fmt.Println(i.String())
  8. }

<kbd>playground</kbd>

huangapple
  • 本文由 发表于 2014年7月22日 01:31:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/24871177.html
匿名

发表评论

匿名网友

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

确定