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
As far as I'm aware there's no OS-independent mechanism to queryOn 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?
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:No such thing exists. Depending how fast you want to generated unique
> Actually, I'd also need a "goroutine id" if something like that even exists,
> right?
IDs, you could try something like this:
http://nf.id.au/concurrency-patterns-a-source-of-unique-numbe
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())
}
}
goinstall tideland-cgl.googlecode.com/hg.
Take a look at http://code.google.com/p/tideland-cgl/source/browse/cgl.go line 86ff.
mue
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