UUID

396 views
Skip to first unread message

Bobby Rullo

unread,
May 17, 2011, 7:49:21 PM5/17/11
to golang-nuts
I'm trying to find something that will help me generate GUIDS, so I want to incorporate time, process ID and something unique to that machine - it's that third one I'm struggling with. Any ideas on how I can get an IP or a MAC address or something from Go?

Thanks!

Bobby

Bobby Rullo

unread,
May 17, 2011, 7:55:18 PM5/17/11
to golang-nuts
Actually, I'd also need a "goroutine id" if something like that even exists, right?

Andrew Gerrand

unread,
May 17, 2011, 7:57:54 PM5/17/11
to Bobby Rullo, golang-nuts

As far as I'm aware there's no OS-independent mechanism to query
network interfaces. I'd just write a wrapper to ifconfig for linux and
darwin and worry about windows later.

On 18 May 2011 09:55, Bobby Rullo <bobby...@google.com> wrote:
> Actually, I'd also need a "goroutine id" if something like that even exists,
> right?

No such thing exists. Depending how fast you want to generated unique
IDs, you could try something like this:

http://nf.id.au/concurrency-patterns-a-source-of-unique-numbe

Andrew

Bobby Rullo

unread,
May 17, 2011, 8:04:58 PM5/17/11
to Andrew Gerrand, golang-nuts
On Tue, May 17, 2011 at 4:57 PM, Andrew Gerrand <a...@golang.org> wrote:
On 18 May 2011 09:49, Bobby Rullo <bobby...@google.com> wrote:
> I'm trying to find something that will help me generate GUIDS, so I want to
> incorporate time, process ID and something unique to that machine - it's
> that third one I'm struggling with. Any ideas on how I can get an IP or a
> MAC address or something from Go?

As far as I'm aware there's no OS-independent mechanism to query
network interfaces. I'd just write a wrapper to ifconfig for linux and
darwin and worry about windows later.


I'll look into that. For my purposes, only Linux matters. 

 
On 18 May 2011 09:55, Bobby Rullo <bobby...@google.com> wrote:
> Actually, I'd also need a "goroutine id" if something like that even exists,
> right?

No such thing exists. Depending how fast you want to generated unique
IDs, you could try something like this:

http://nf.id.au/concurrency-patterns-a-source-of-unique-numbe


Neat, I might give that a shot.

Thanks!
 

Russ Cox

unread,
May 17, 2011, 8:13:04 PM5/17/11
to Bobby Rullo, Andrew Gerrand, golang-nuts
It would be even easier to use type-4 (random) UUIDs.

package main

import (
"crypto/rand"
"fmt"
"io"
"log"
)

func uuid() string {
b := make([]byte, 16)
_, err := io.ReadFull(rand.Reader, b)
if err != nil {
log.Fatal(err)
}
b[6] = (b[6] & 0x0F) | 0x40
b[8] = (b[8] &^ 0x40) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", b[:4], b[4:6], b[6:8], b[8:10], b[10:])
}

func main() {
for i := 0; i < 10; i++ {
fmt.Println(uuid())
}
}

Mue

unread,
May 18, 2011, 4:20:05 AM5/18/11
to golan...@googlegroups.com
Hi,

for v4 UUIDs (w/o MAC address) just
goinstall tideland-cgl.googlecode.com/hg.

Take a look at http://code.google.com/p/tideland-cgl/source/browse/cgl.go line 86ff.

mue

Russ Cox

unread,
May 18, 2011, 7:25:44 AM5/18/11
to golan...@googlegroups.com

i strongly advise against using that code for generating UUIDs.
the continual reseeding of rand means that it generates at most
2^64 distinct uuids. the particular choice of seed has maybe
2^30 possible values for a given run (up to heap size), and
it is strongly correlated with the last seed used, so strongly that
collisions are nearly certain.

the code i posted uses crypto/rand for exactly this reason.

mini=; cat x.go
package main

import "tideland-cgl.googlecode.com/hg"

func main() {
m := make(map[string]bool)
for i := 0;; i++ {
u := cgl.NewUUID().String()
if m[u] {
println("collision after", i)
return
}
m[u] = true
}
}
mini=; for i in $(seq 10); do 6.out; done
collision after 114
collision after 295
collision after 244
collision after 105
collision after 149
collision after 217
collision after 68
collision after 162
collision after 150
collision after 86
mini=;

russ

Mue

unread,
May 18, 2011, 10:19:08 AM5/18/11
to golan...@googlegroups.com
Oh, great analysis and hint. Thanks, Russ, I'll change my code immediately.

mue
Reply all
Reply to author
Forward
0 new messages