Generating unique file name using UUID in golang

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

Generating unique file name using UUID in golang

问题

我需要使用UUID1生成一个唯一的文件名。

我的当前Python代码是:

uuid.uuid1().hex[:16] // 我需要一个长度为16的字符文件名

Golang中的等效代码是什么?

谢谢!

英文:

I need to generate an unique file name with UUID1.

My current python code is:

uuid.uuid1().hex[:16] // i need 16 chars file name

What could be the golang equivalent?

Thanks!

答案1

得分: 6

在Go的标准库中没有guid或uuid类型,但有其他一些方法可以实现,比如使用第三方包,例如:https://godoc.org/code.google.com/p/go-uuid/uuid 或 https://github.com/nu7hatch/gouuid

import "github.com/nu7hatch/gouuid"

id, err := uuid.NewV4()

这个答案还提供了另一种选项,可以使用Unix命令行工具生成UUID,参考链接:https://stackoverflow.com/questions/15130321/is-there-a-method-to-generate-a-uuid-with-go-language,尽管性能似乎不太好。

英文:

There isn't a guid or uuid type in the standard library for Go but there are some other ways to do it, like using a third party package such as this; https://godoc.org/code.google.com/p/go-uuid/uuid or https://github.com/nu7hatch/gouuid

import "github.com/nu7hatch/gouuid"

id, err := uuid.NewV4()

This answer has another option as well which makes use of Unix command line utils; https://stackoverflow.com/questions/15130321/is-there-a-method-to-generate-a-uuid-with-go-language though it doesn't seem to perform very well.

答案2

得分: 5

我相信你在问题陈述中存在阻抗不匹配,并且你的Python代码不会按照你的期望工作。

根据“https://stackoverflow.com/questions/15130321/is-there-a-method-to-generate-a-uuid-with-go-language”上的一些答案以及https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_(date-time_and_MAC_address)中明确描述的内容,UUIDs只有在完整地使用时才可能是唯一的,而不是部分使用,并且并不一定是随机的,特别是版本1实际上是相当可预测的,因为它基于生成它的主机的日期/时间和MAC地址。

因此,最好的方法可能是使用类似于先前提到的问题的答案中的代码,根据crypto/rand自己的规范生成一个随机文件名,并且不要滥用那些并不一定提供所需随机性的库。

以下是示例代码:

package main

import (
	"crypto/rand"
	"fmt"
)

func random_filename_16_char() (s string, err error) {
	b := make([]byte, 8)
	_, err = rand.Read(b)
	if err != nil {
		return
	}
	s = fmt.Sprintf("%x", b)
	return
}

func main() {
	s, _ := random_filename_16_char()
	fmt.Println(s)
}

希望对你有帮助!

英文:

I believe you have an impedance mismatch in your problem statement, and your Python code would not work as you expect it should.

As can be deduced by some answers at “https://stackoverflow.com/questions/15130321/is-there-a-method-to-generate-a-uuid-with-go-language”, as well as is clearly described in https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_(date-time_and_MAC_address), UUIDs are very likely to be unique only when taken in full, not in parts, and are not necessarily random at all, especially with the Version 1 actually being rather predictable, as it's based on the date/time and MAC address of the host generating it.

As such, the best approach may be to use something similar to the code in one of the answers to the prior mentioned question, to actually generate a random filename based on crypto/rand yourself to your own specification, and without the misuse of the libraries not necessarily intended to provide the required randomness for the task at hand.

https://play.golang.org/p/k2V-Mc5Y31e

package main

import (
	"crypto/rand"
	"fmt"
)

func random_filename_16_char() (s string, err error) {
	b := make([]byte, 8)
	_, err = rand.Read(b)
	if err != nil {
		return
	}
	s = fmt.Sprintf("%x", b)
	return
}

func main() {
	s, _ := random_filename_16_char()
	fmt.Println(s)
}

huangapple
  • 本文由 发表于 2015年8月29日 01:37:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/32276773.html
匿名

发表评论

匿名网友

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

确定