how to generate UUID / GUID?

10,139 views
Skip to first unread message

ygl

unread,
Feb 24, 2010, 12:54:19 PM2/24/10
to golang-nuts
Hello,

How should i generate network-wide unique ids (UUID/GUID) in Go? ids
for components in a distributed system.

Thanks
Yigong

Russ Cox

unread,
Feb 24, 2010, 1:44:09 PM2/24/10
to ygl, golang-nuts
> How should i generate network-wide unique ids (UUID/GUID) in Go? ids
> for components in a distributed system.

There's no official library. Ignoring error checking,
this seems like it would work fine:

f, _ := os.Open("/dev/urandom", os.O_RDONLY, 0)
b := make([]byte, 16)
f.Read(b)
f.Close()
uuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])

Russ

Mue

unread,
Feb 24, 2010, 2:40:44 PM2/24/10
to golang-nuts

Ryanne Dolan

unread,
Feb 24, 2010, 2:49:18 PM2/24/10
to Mue, golang-nuts
I wouldn't use time.Nanoseconds nor runtime.MemStats.Alloc to create a GUID, since these have a good chance of being exactly the same for a group of similar programs.

Thanks.
Ryanne

--
www.ryannedolan.info

ygl

unread,
Feb 24, 2010, 3:42:41 PM2/24/10
to golang-nuts
Thanks all for the code and pointers.

which may provide more guarantee of uniquess: uuid generated by
reading "/dev/urandom", or generated by calling rand.Intn?

Yigong

Dmitri Sosnik

unread,
Feb 24, 2010, 3:56:37 PM2/24/10
to Russ Cox, ygl, golang-nuts
Why wouldn't just use /usr/bin/uuidgen?

D

Russ Cox

unread,
Feb 24, 2010, 4:13:02 PM2/24/10
to ygl, golang-nuts
On Wed, Feb 24, 2010 at 12:42, ygl <ygl...@gmail.com> wrote:
> which may provide more guarantee of uniquess: uuid generated by
> reading "/dev/urandom", or generated by calling rand.Intn?

You've probably got a better chance with /dev/urandom.
rand is really for pseudo-random numbers for things like
simulations. Eventually there should be a crypto/rand
for generating random byte sequences, but /dev/urandom
is fine for now.

On Wed, Feb 24, 2010 at 12:56, Dmitri Sosnik <dim...@gmail.com> wrote:
> Why wouldn't just use /usr/bin/uuidgen?

Why depend on a binary that might not exist
when the code to execute it and parse the output
would be longer than the code it replaces?

Russ

Mue

unread,
Feb 24, 2010, 4:22:20 PM2/24/10
to golang-nuts
Sure this may happen, but the risk of equal combinations of time and
memory allocation on two systems seems to be relative small. But I
hope that a v5 uuid lib based on sha-1 will be part of the standard
libs soon. *smile*

mue

On 24 Feb., 20:49, Ryanne Dolan <ryannedo...@gmail.com> wrote:
> I wouldn't use time.Nanoseconds nor runtime.MemStats.Alloc to create a GUID,
> since these have a good chance of being exactly the same for a group of
> similar programs.
>
> Thanks.
> Ryanne
>
> --www.ryannedolan.info
>
>
>
> On Wed, Feb 24, 2010 at 1:40 PM, Mue <m...@tideland.biz> wrote:
> > You'll find my UUID v4 implementation at
>

> >http://code.google.com/p/tideland-cgl/source/browse/trunk/src/pkg/tid...

Kevin Ballard

unread,
Feb 24, 2010, 4:50:52 PM2/24/10
to Mue, golang-nuts
I believe OS X's uuids are generated by combining both the time and a
hardware value (usually the ethernet MAC address). This seems like a
good model to follow, if Go actually provides access to the current
device's MAC address.

-Kevin Ballard

--
Kevin Ballard
http://kevin.sb.org
kbal...@gmail.com

Ryanne Dolan

unread,
Feb 24, 2010, 4:51:52 PM2/24/10
to Mue, golang-nuts

Mue,

Two identical programs starting at only slightly different times could still end up with the same seed using your method. What's worse is that two identical programs running on two different machines could start at nearly the same time, or worse, two similar programs starting as similar times...

Especially considering that Nanoseconds isn't always accurate to more than a second, and Alloc might not be called yet, this is a bad idea.  Unless I am not understanding what those fxns do. I recommend using a MAC address and time if possible.

- from my phone -

On Feb 24, 2010 3:22 PM, "Mue" <m...@tideland.biz> wrote:

Sure this may happen, but the risk of equal combinations of time and
memory allocation on two systems seems to be relative small. But I
hope that a v5 uuid lib based on sha-1 will be part of the standard
libs soon. *smile*

mue


On 24 Feb., 20:49, Ryanne Dolan <ryannedo...@gmail.com> wrote:

> I wouldn't use time.Nanoseconds no...

> On Wed, Feb 24, 2010 at 1:40 PM, Mue <m...@tideland.biz> wrote:

> > You'll find my UUID v4 impleme...

> >http://code.google.com/p/tideland-cgl/source/browse/trunk/src/pkg/tid...

> > .
>
> > mue
>
> > On 24 Feb., 19:44, Russ Cox <r...@golang.org> wrote:

> > > > How should i gene...

te...@ashishbanerjee.com

unread,
May 13, 2012, 12:43:32 AM5/13/12
to golan...@googlegroups.com
Here is a program for generating UUID 

Paul Borman

unread,
May 14, 2012, 1:14:22 PM5/14/12
to te...@ashishbanerjee.com, golan...@googlegroups.com
There are also at least two packages out there for UUID generation/inspection.  
Reply all
Reply to author
Forward
0 new messages