How should i generate network-wide unique ids (UUID/GUID) in Go? ids
for components in a distributed system.
Thanks
Yigong
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
which may provide more guarantee of uniquess: uuid generated by
reading "/dev/urandom", or generated by calling rand.Intn?
Yigong
D
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
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
--
Kevin Ballard
http://kevin.sb.org
kbal...@gmail.com
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...