Is there a method to generate a UUID with go language
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have code that looks like this:
u := make(byte, 16)
_, err := rand.Read(u)
if err != nil {
return
}
u[8] = (u[8] | 0x80) & 0xBF // what does this do?
u[6] = (u[6] | 0x40) & 0x4F // what does this do?
return hex.EncodeToString(u)
It returns a string with a length of 32, but I don't think it is a valid UUID. If it is a real UUID, why is it a UUID, and what is the purpose of the code that modifies the value of u[8] and u[6].
Is there a better way of generating UUIDs?
go uuid
add a comment |
I have code that looks like this:
u := make(byte, 16)
_, err := rand.Read(u)
if err != nil {
return
}
u[8] = (u[8] | 0x80) & 0xBF // what does this do?
u[6] = (u[6] | 0x40) & 0x4F // what does this do?
return hex.EncodeToString(u)
It returns a string with a length of 32, but I don't think it is a valid UUID. If it is a real UUID, why is it a UUID, and what is the purpose of the code that modifies the value of u[8] and u[6].
Is there a better way of generating UUIDs?
go uuid
This answer seems more apt now.
– ViKiG
Apr 4 at 14:29
add a comment |
I have code that looks like this:
u := make(byte, 16)
_, err := rand.Read(u)
if err != nil {
return
}
u[8] = (u[8] | 0x80) & 0xBF // what does this do?
u[6] = (u[6] | 0x40) & 0x4F // what does this do?
return hex.EncodeToString(u)
It returns a string with a length of 32, but I don't think it is a valid UUID. If it is a real UUID, why is it a UUID, and what is the purpose of the code that modifies the value of u[8] and u[6].
Is there a better way of generating UUIDs?
go uuid
I have code that looks like this:
u := make(byte, 16)
_, err := rand.Read(u)
if err != nil {
return
}
u[8] = (u[8] | 0x80) & 0xBF // what does this do?
u[6] = (u[6] | 0x40) & 0x4F // what does this do?
return hex.EncodeToString(u)
It returns a string with a length of 32, but I don't think it is a valid UUID. If it is a real UUID, why is it a UUID, and what is the purpose of the code that modifies the value of u[8] and u[6].
Is there a better way of generating UUIDs?
go uuid
go uuid
edited Aug 30 '15 at 1:17
Flimzy
41k1367101
41k1367101
asked Feb 28 '13 at 7:58
hardPasshardPass
5,053122839
5,053122839
This answer seems more apt now.
– ViKiG
Apr 4 at 14:29
add a comment |
This answer seems more apt now.
– ViKiG
Apr 4 at 14:29
This answer seems more apt now.
– ViKiG
Apr 4 at 14:29
This answer seems more apt now.
– ViKiG
Apr 4 at 14:29
add a comment |
13 Answers
13
active
oldest
votes
u[8] = (u[8] | 0x80) & 0xBF // what's the purpose ?
u[6] = (u[6] | 0x40) & 0x4F // what's the purpose ?
These lines clamp the values of byte 6 and 8 to a specific range. rand.Read returns random bytes in the range 0-255, which are not all valid values for a UUID. As far as I can tell, this should be done for all the values in the slice though.
If you are on linux, you can alternatively call /usr/bin/uuidgen.
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
out, err := exec.Command("uuidgen").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", out)
}
Which yields:
$ go run uuid.go
dc9076e9-2fda-4019-bd2c-900a8284b9c4
18
Notably, this approach is slow; on a 2012 MacBook Air this strategy can produce only 170 uuids/second.
– Jay Taylor
Aug 25 '13 at 22:35
8
And using the nu7hatch/gouuid library, I was able to generate 172,488 uuids/second.
– Jay Taylor
Aug 25 '13 at 23:09
1
Good explanation of theu[6]andu[8]bytes.
– chowey
May 6 '14 at 17:26
3
On my system (Ubuntu 15.10) I also needed to run the command output through strings.Trim(string(out)) to remove the newline character, otherwise it was inputted up as a trailing ? character in the filesystem.
– gregtzar
Feb 24 '16 at 1:06
24
Calling an external program which may or may not exist is an awful way to do this fairly simple task.
– Timmmm
Apr 4 '16 at 10:06
|
show 3 more comments
You can generate UUIDs using the go-uuid library. This can be installed with:
go get github.com/nu7hatch/gouuid
You can generate random (version 4) UUIDs with:
import "github.com/nu7hatch/gouuid"
...
u, err := uuid.NewV4()
The returned UUID type is a 16 byte array, so you can retrieve the binary value easily. It also provides the standard hex string representation via its String() method.
The code you have also looks like it will also generate a valid version 4 UUID: the bitwise manipulation you perform at the end set the version and variant fields of the UUID to correctly identify it as version 4. This is done to distinguish random UUIDs from ones generated via other algorithms (e.g. version 1 UUIDs based on your MAC address and time).
2
@Flimzy for people who don't know what they are doing that's most likely true. Introducing unnecessary dependencies is always a bad thing.
– Erik Aigner
Sep 3 '15 at 16:25
27
@ErikAigner As long as it is 50 lines I do not have to think about, write and test, I'll take them thank you.. I have other stuff to do then reinvent the wheel.
– RickyA
Nov 2 '15 at 14:48
1
This library looks like it is not actually RFC4122 compliant: github.com/nu7hatch/gouuid/issues/28 (currently open issue as of 2/1/2016)
– Charles L.
Feb 1 '16 at 20:21
2
@ErikAigner i just find that riduclous. No one reinvents stuff thats done already unless you can do better or need something specific to your program, if you inspect the code and see it does it well why bother do it yourself - not only do you waste development time and cost, you are also potentially going to bring in bugs or simply wrong implementations if you don't know completely what you are doing, these libraries are usually made people who do know what they are doing. It is not rookie to use third party libraries, its only rookie to just assume it works and not inspect the code first..
– Sir
Oct 31 '17 at 21:50
2
Oh im sorry i forgot every company ever reprograms absolutely everything, makes their own editors, operating systems, their own programming language because why use Go when you should know how to make your own right?. No one does. At the end of the day you will be using some one else's code some where unless you have no concept of time/money saving as a business. Its just totally dumb to do it twice over. Many companies use code with MIT licenses and then modify to their needs. Clearly you don't have much experience working in different companies.
– Sir
Nov 2 '17 at 19:16
|
show 5 more comments
The go-uuid library is NOT RFC4122 compliant. The variant bits are not set correctly. There have been several attempts by community members to have this fixed but pull requests for the fix are not being accepted.
You can generate UUIDs using the Go uuid library I rewrote based on the go-uuid library. There are several fixes and improvements. This can be installed with:
go get github.com/twinj/uuid
You can generate random (version 4) UUIDs with:
import "github.com/twinj/uuid"
u := uuid.NewV4()
The returned UUID type is an interface and the underlying type is an array.
The library also generates v1 UUIDs and correctly generates v3 and 5 UUIDs. There are several new methods to help with printing and formatting and also new general methods to create UUIDs based off of existing data.
4
I like this package. I've officially adopted it for all my applications. I found that the nu7hatch package wasn't RFC4122-compliant.
– Richard Eng
Aug 19 '14 at 16:47
+1 Agreed, the updates and printing/formatting extensions already included.
– eduncan911
Sep 10 '14 at 16:53
4
Disclaimer missing? :p
– chakrit
Sep 22 '14 at 7:23
2
What is the library "below"? You should avoid using above and below on SO as that can change quite quickly.
– inf
Jan 18 '15 at 7:10
There's also another equivallent, satori/go.uuid. Didn't try it yet but I am going to use it as a replacement of nu7hatch dead project...
– shadyyx
Apr 7 '16 at 13:06
add a comment |
"crypto/rand" is cross platform pkg for random bytes generattion
package main
import (
"crypto/rand"
"fmt"
)
func pseudo_uuid() (uuid string) {
b := make(byte, 16)
_, err := rand.Read(b)
if err != nil {
fmt.Println("Error: ", err)
return
}
uuid = fmt.Sprintf("%X-%X-%X-%X-%X", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return
}
2
pseudo_uuidbecause it is missing the non-random identifiers like MAC address and whatever else RFC4122 specified? So it's actually more random.
– Xeoncross
Apr 21 '17 at 21:27
2
good answer; I've expanded it at stackoverflow.com/a/48134820/1122270, and I think a lot of folks actually don't need to use UUIDs specifically (nor the sha1/sha256 that I thought I need to use for my own random-id problem), but simply want something random and unique, and your sample provides a good start for a solution
– cnst
Jan 7 '18 at 6:26
Thanks! Simple enough
– Karl Pokus
Apr 3 at 9:32
add a comment |
gofrs/uuid is the replacement for satori/go.uuid, which is the most starred UUID package for Go. It supports UUID versions 1-5 and is RFC 4122 and DCE 1.1 compliant.
import "github.com/gofrs/uuid"
// Create a Version 4 UUID, panicking on error
u := uuid.Must(uuid.NewV4())
add a comment |
From Russ Cox's post:
There's no official library. Ignoring error checking,
this seems like it would work fine:
f, _ := os.Open("/dev/urandom")
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:])
Note: In the original, pre Go 1 version the first line was:
f, _ := os.Open("/dev/urandom", os.O_RDONLY, 0)
Here it compiles and executes, only /dev/urandom returns all zeros in the playground. Should work fine locally.
In the same thread there are some other methods/references/packages found.
10
This won't generate a valid UUID though: version 4 UUIDs (the type based on random data) require a few bits to be set in a certain way to avoid conflicting with the non-random UUID formats.
– James Henstridge
Feb 28 '13 at 11:21
4
Better to useimport "crypto/rand"in my opinion, but +1 foruuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]). Combined with the OP's code, and this works great.
– chowey
May 6 '14 at 17:25
1
Using the crypto/rand package: play.golang.org/p/7JJDx4GL77. zzzz's code does what crypt/rand does, except that it also covers platforms that don't support /dev/urandom (Windows).
– Drew
Nov 4 '14 at 6:09
It should be noted that this is platform specific
– Dan Esparza
Oct 27 '16 at 18:03
1
@Matt: the problem is that the other UUID formats get their uniqueness by delegating to some other authority (e.g. that your Ethernet MAC address is unique), and then combining that with something else (e.g. the time plus a counter). If you produce a random UUID that isn't correctly formatted as a V4 one, you're weakening the system.
– James Henstridge
Sep 2 '17 at 9:45
|
show 3 more comments
As part of the uuid spec, if you generate a uuid from random it must contain a "4" as the 13th character and a "8", "9", "a", or "b" in the 17th (source).
// this makes sure that the 13th character is "4"
u[6] = (u[6] | 0x40) & 0x4F
// this makes sure that the 17th is "8", "9", "a", or "b"
u[8] = (u[8] | 0x80) & 0xBF
add a comment |
An official implementation by Google is currently under development but not stable yet: https://github.com/google/uuid
Looks like it's the continuation of https://github.com/pborman/uuid from this answer
Generating a version 4 UUID works like this:
uuid, err := uuid.NewRandom()
Update:
A version 1.0.0 has been released and the note regarding possible instability has been removed.
add a comment |
The gorand package has a UUID method that returns a Version 4 (randomly generated) UUID in its canonical string representation ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") and it's RFC 4122 compliant.
It also uses the crypto/rand package to ensure the most cryptographically secure generation of UUIDs across all platforms supported by Go.
import "github.com/leonelquinteros/gorand"
func main() {
uuid, err := gorand.UUID()
if err != nil {
panic(err.Error())
}
println(uuid)
}
add a comment |
You should use google/uuid https://github.com/google/uuid
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
id, err := uuid.NewUUID()
if err !=nil {
// handle error
}
fmt.Printf(id.String())
}
This package is RFC4122 and DCE 1.1 compliant
2
I believe this answer should be on the top since this seems the most correct solution for current time. Accepted answer does not have the most upvotes and top 2 upvoted answers mention packages which might not be better thangoogle/uuid
– ViKiG
Apr 4 at 14:27
add a comment |
On Linux, you can read from /proc/sys/kernel/random/uuid:
package main
import "io/ioutil"
import "fmt"
func main() {
u, _ := ioutil.ReadFile("/proc/sys/kernel/random/uuid")
fmt.Println(string(u))
}
No external dependencies!
$ go run uuid.go
3ee995e3-0c96-4e30-ac1e-f7f04fd03e44
1
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:08
add a comment |
This library is our standard for uuid generation and parsing:
https://github.com/pborman/uuid
add a comment |
For Windows, I did recently this:
// +build windows
package main
import (
"syscall"
"unsafe"
)
var (
modrpcrt4 = syscall.NewLazyDLL("rpcrt4.dll")
procUuidCreate = modrpcrt4.NewProc("UuidCreate")
)
const (
RPC_S_OK = 0
)
func NewUuid() (byte, error) {
var uuid [16]byte
rc, _, e := syscall.Syscall(procUuidCreate.Addr(), 1,
uintptr(unsafe.Pointer(&uuid[0])), 0, 0)
if int(rc) != RPC_S_OK {
if e != 0 {
return nil, error(e)
} else {
return nil, syscall.EINVAL
}
}
return uuid[:], nil
}
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:10
@Byebye, I wonder why you consider yourself an authority to decide what "is worse" (and what is not) to skim through all the answers given to this question and downvote all which are "system-dependent"? These answers were given to a) widen the horizon of all possible choices, and b) collectively present a complete picture. So please stop childishly "playing SO" and put some thought before you action.
– kostix
Aug 7 '18 at 8:50
Short answer. Writing maintainable code. Your answer cannot be ported to a different platform. So if the OP would choose to move their application to another platform the application would break. I've had my fair share of people who wrote platform dependent code where it is totally unnecessary and it creates more trouble then it's worth. You don't write code for just yourself. You write code for the people who will maintain it after you're gone. That's why this answer is not appropriate. No reason to resort to ad hominems and call me childish.
– Byebye
Aug 7 '18 at 9:12
1
@Byebye, I overreacted, so please excuse me for the attack. I'm not convinced of your reasons, still, but supposedly it's that "let's agree to disagree" case.
– kostix
Aug 7 '18 at 9:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15130321%2fis-there-a-method-to-generate-a-uuid-with-go-language%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
u[8] = (u[8] | 0x80) & 0xBF // what's the purpose ?
u[6] = (u[6] | 0x40) & 0x4F // what's the purpose ?
These lines clamp the values of byte 6 and 8 to a specific range. rand.Read returns random bytes in the range 0-255, which are not all valid values for a UUID. As far as I can tell, this should be done for all the values in the slice though.
If you are on linux, you can alternatively call /usr/bin/uuidgen.
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
out, err := exec.Command("uuidgen").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", out)
}
Which yields:
$ go run uuid.go
dc9076e9-2fda-4019-bd2c-900a8284b9c4
18
Notably, this approach is slow; on a 2012 MacBook Air this strategy can produce only 170 uuids/second.
– Jay Taylor
Aug 25 '13 at 22:35
8
And using the nu7hatch/gouuid library, I was able to generate 172,488 uuids/second.
– Jay Taylor
Aug 25 '13 at 23:09
1
Good explanation of theu[6]andu[8]bytes.
– chowey
May 6 '14 at 17:26
3
On my system (Ubuntu 15.10) I also needed to run the command output through strings.Trim(string(out)) to remove the newline character, otherwise it was inputted up as a trailing ? character in the filesystem.
– gregtzar
Feb 24 '16 at 1:06
24
Calling an external program which may or may not exist is an awful way to do this fairly simple task.
– Timmmm
Apr 4 '16 at 10:06
|
show 3 more comments
u[8] = (u[8] | 0x80) & 0xBF // what's the purpose ?
u[6] = (u[6] | 0x40) & 0x4F // what's the purpose ?
These lines clamp the values of byte 6 and 8 to a specific range. rand.Read returns random bytes in the range 0-255, which are not all valid values for a UUID. As far as I can tell, this should be done for all the values in the slice though.
If you are on linux, you can alternatively call /usr/bin/uuidgen.
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
out, err := exec.Command("uuidgen").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", out)
}
Which yields:
$ go run uuid.go
dc9076e9-2fda-4019-bd2c-900a8284b9c4
18
Notably, this approach is slow; on a 2012 MacBook Air this strategy can produce only 170 uuids/second.
– Jay Taylor
Aug 25 '13 at 22:35
8
And using the nu7hatch/gouuid library, I was able to generate 172,488 uuids/second.
– Jay Taylor
Aug 25 '13 at 23:09
1
Good explanation of theu[6]andu[8]bytes.
– chowey
May 6 '14 at 17:26
3
On my system (Ubuntu 15.10) I also needed to run the command output through strings.Trim(string(out)) to remove the newline character, otherwise it was inputted up as a trailing ? character in the filesystem.
– gregtzar
Feb 24 '16 at 1:06
24
Calling an external program which may or may not exist is an awful way to do this fairly simple task.
– Timmmm
Apr 4 '16 at 10:06
|
show 3 more comments
u[8] = (u[8] | 0x80) & 0xBF // what's the purpose ?
u[6] = (u[6] | 0x40) & 0x4F // what's the purpose ?
These lines clamp the values of byte 6 and 8 to a specific range. rand.Read returns random bytes in the range 0-255, which are not all valid values for a UUID. As far as I can tell, this should be done for all the values in the slice though.
If you are on linux, you can alternatively call /usr/bin/uuidgen.
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
out, err := exec.Command("uuidgen").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", out)
}
Which yields:
$ go run uuid.go
dc9076e9-2fda-4019-bd2c-900a8284b9c4
u[8] = (u[8] | 0x80) & 0xBF // what's the purpose ?
u[6] = (u[6] | 0x40) & 0x4F // what's the purpose ?
These lines clamp the values of byte 6 and 8 to a specific range. rand.Read returns random bytes in the range 0-255, which are not all valid values for a UUID. As far as I can tell, this should be done for all the values in the slice though.
If you are on linux, you can alternatively call /usr/bin/uuidgen.
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
out, err := exec.Command("uuidgen").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", out)
}
Which yields:
$ go run uuid.go
dc9076e9-2fda-4019-bd2c-900a8284b9c4
answered Feb 28 '13 at 8:36
jimtjimt
17.5k55253
17.5k55253
18
Notably, this approach is slow; on a 2012 MacBook Air this strategy can produce only 170 uuids/second.
– Jay Taylor
Aug 25 '13 at 22:35
8
And using the nu7hatch/gouuid library, I was able to generate 172,488 uuids/second.
– Jay Taylor
Aug 25 '13 at 23:09
1
Good explanation of theu[6]andu[8]bytes.
– chowey
May 6 '14 at 17:26
3
On my system (Ubuntu 15.10) I also needed to run the command output through strings.Trim(string(out)) to remove the newline character, otherwise it was inputted up as a trailing ? character in the filesystem.
– gregtzar
Feb 24 '16 at 1:06
24
Calling an external program which may or may not exist is an awful way to do this fairly simple task.
– Timmmm
Apr 4 '16 at 10:06
|
show 3 more comments
18
Notably, this approach is slow; on a 2012 MacBook Air this strategy can produce only 170 uuids/second.
– Jay Taylor
Aug 25 '13 at 22:35
8
And using the nu7hatch/gouuid library, I was able to generate 172,488 uuids/second.
– Jay Taylor
Aug 25 '13 at 23:09
1
Good explanation of theu[6]andu[8]bytes.
– chowey
May 6 '14 at 17:26
3
On my system (Ubuntu 15.10) I also needed to run the command output through strings.Trim(string(out)) to remove the newline character, otherwise it was inputted up as a trailing ? character in the filesystem.
– gregtzar
Feb 24 '16 at 1:06
24
Calling an external program which may or may not exist is an awful way to do this fairly simple task.
– Timmmm
Apr 4 '16 at 10:06
18
18
Notably, this approach is slow; on a 2012 MacBook Air this strategy can produce only 170 uuids/second.
– Jay Taylor
Aug 25 '13 at 22:35
Notably, this approach is slow; on a 2012 MacBook Air this strategy can produce only 170 uuids/second.
– Jay Taylor
Aug 25 '13 at 22:35
8
8
And using the nu7hatch/gouuid library, I was able to generate 172,488 uuids/second.
– Jay Taylor
Aug 25 '13 at 23:09
And using the nu7hatch/gouuid library, I was able to generate 172,488 uuids/second.
– Jay Taylor
Aug 25 '13 at 23:09
1
1
Good explanation of the
u[6] and u[8] bytes.– chowey
May 6 '14 at 17:26
Good explanation of the
u[6] and u[8] bytes.– chowey
May 6 '14 at 17:26
3
3
On my system (Ubuntu 15.10) I also needed to run the command output through strings.Trim(string(out)) to remove the newline character, otherwise it was inputted up as a trailing ? character in the filesystem.
– gregtzar
Feb 24 '16 at 1:06
On my system (Ubuntu 15.10) I also needed to run the command output through strings.Trim(string(out)) to remove the newline character, otherwise it was inputted up as a trailing ? character in the filesystem.
– gregtzar
Feb 24 '16 at 1:06
24
24
Calling an external program which may or may not exist is an awful way to do this fairly simple task.
– Timmmm
Apr 4 '16 at 10:06
Calling an external program which may or may not exist is an awful way to do this fairly simple task.
– Timmmm
Apr 4 '16 at 10:06
|
show 3 more comments
You can generate UUIDs using the go-uuid library. This can be installed with:
go get github.com/nu7hatch/gouuid
You can generate random (version 4) UUIDs with:
import "github.com/nu7hatch/gouuid"
...
u, err := uuid.NewV4()
The returned UUID type is a 16 byte array, so you can retrieve the binary value easily. It also provides the standard hex string representation via its String() method.
The code you have also looks like it will also generate a valid version 4 UUID: the bitwise manipulation you perform at the end set the version and variant fields of the UUID to correctly identify it as version 4. This is done to distinguish random UUIDs from ones generated via other algorithms (e.g. version 1 UUIDs based on your MAC address and time).
2
@Flimzy for people who don't know what they are doing that's most likely true. Introducing unnecessary dependencies is always a bad thing.
– Erik Aigner
Sep 3 '15 at 16:25
27
@ErikAigner As long as it is 50 lines I do not have to think about, write and test, I'll take them thank you.. I have other stuff to do then reinvent the wheel.
– RickyA
Nov 2 '15 at 14:48
1
This library looks like it is not actually RFC4122 compliant: github.com/nu7hatch/gouuid/issues/28 (currently open issue as of 2/1/2016)
– Charles L.
Feb 1 '16 at 20:21
2
@ErikAigner i just find that riduclous. No one reinvents stuff thats done already unless you can do better or need something specific to your program, if you inspect the code and see it does it well why bother do it yourself - not only do you waste development time and cost, you are also potentially going to bring in bugs or simply wrong implementations if you don't know completely what you are doing, these libraries are usually made people who do know what they are doing. It is not rookie to use third party libraries, its only rookie to just assume it works and not inspect the code first..
– Sir
Oct 31 '17 at 21:50
2
Oh im sorry i forgot every company ever reprograms absolutely everything, makes their own editors, operating systems, their own programming language because why use Go when you should know how to make your own right?. No one does. At the end of the day you will be using some one else's code some where unless you have no concept of time/money saving as a business. Its just totally dumb to do it twice over. Many companies use code with MIT licenses and then modify to their needs. Clearly you don't have much experience working in different companies.
– Sir
Nov 2 '17 at 19:16
|
show 5 more comments
You can generate UUIDs using the go-uuid library. This can be installed with:
go get github.com/nu7hatch/gouuid
You can generate random (version 4) UUIDs with:
import "github.com/nu7hatch/gouuid"
...
u, err := uuid.NewV4()
The returned UUID type is a 16 byte array, so you can retrieve the binary value easily. It also provides the standard hex string representation via its String() method.
The code you have also looks like it will also generate a valid version 4 UUID: the bitwise manipulation you perform at the end set the version and variant fields of the UUID to correctly identify it as version 4. This is done to distinguish random UUIDs from ones generated via other algorithms (e.g. version 1 UUIDs based on your MAC address and time).
2
@Flimzy for people who don't know what they are doing that's most likely true. Introducing unnecessary dependencies is always a bad thing.
– Erik Aigner
Sep 3 '15 at 16:25
27
@ErikAigner As long as it is 50 lines I do not have to think about, write and test, I'll take them thank you.. I have other stuff to do then reinvent the wheel.
– RickyA
Nov 2 '15 at 14:48
1
This library looks like it is not actually RFC4122 compliant: github.com/nu7hatch/gouuid/issues/28 (currently open issue as of 2/1/2016)
– Charles L.
Feb 1 '16 at 20:21
2
@ErikAigner i just find that riduclous. No one reinvents stuff thats done already unless you can do better or need something specific to your program, if you inspect the code and see it does it well why bother do it yourself - not only do you waste development time and cost, you are also potentially going to bring in bugs or simply wrong implementations if you don't know completely what you are doing, these libraries are usually made people who do know what they are doing. It is not rookie to use third party libraries, its only rookie to just assume it works and not inspect the code first..
– Sir
Oct 31 '17 at 21:50
2
Oh im sorry i forgot every company ever reprograms absolutely everything, makes their own editors, operating systems, their own programming language because why use Go when you should know how to make your own right?. No one does. At the end of the day you will be using some one else's code some where unless you have no concept of time/money saving as a business. Its just totally dumb to do it twice over. Many companies use code with MIT licenses and then modify to their needs. Clearly you don't have much experience working in different companies.
– Sir
Nov 2 '17 at 19:16
|
show 5 more comments
You can generate UUIDs using the go-uuid library. This can be installed with:
go get github.com/nu7hatch/gouuid
You can generate random (version 4) UUIDs with:
import "github.com/nu7hatch/gouuid"
...
u, err := uuid.NewV4()
The returned UUID type is a 16 byte array, so you can retrieve the binary value easily. It also provides the standard hex string representation via its String() method.
The code you have also looks like it will also generate a valid version 4 UUID: the bitwise manipulation you perform at the end set the version and variant fields of the UUID to correctly identify it as version 4. This is done to distinguish random UUIDs from ones generated via other algorithms (e.g. version 1 UUIDs based on your MAC address and time).
You can generate UUIDs using the go-uuid library. This can be installed with:
go get github.com/nu7hatch/gouuid
You can generate random (version 4) UUIDs with:
import "github.com/nu7hatch/gouuid"
...
u, err := uuid.NewV4()
The returned UUID type is a 16 byte array, so you can retrieve the binary value easily. It also provides the standard hex string representation via its String() method.
The code you have also looks like it will also generate a valid version 4 UUID: the bitwise manipulation you perform at the end set the version and variant fields of the UUID to correctly identify it as version 4. This is done to distinguish random UUIDs from ones generated via other algorithms (e.g. version 1 UUIDs based on your MAC address and time).
answered Feb 28 '13 at 11:33
James HenstridgeJames Henstridge
28.8k58490
28.8k58490
2
@Flimzy for people who don't know what they are doing that's most likely true. Introducing unnecessary dependencies is always a bad thing.
– Erik Aigner
Sep 3 '15 at 16:25
27
@ErikAigner As long as it is 50 lines I do not have to think about, write and test, I'll take them thank you.. I have other stuff to do then reinvent the wheel.
– RickyA
Nov 2 '15 at 14:48
1
This library looks like it is not actually RFC4122 compliant: github.com/nu7hatch/gouuid/issues/28 (currently open issue as of 2/1/2016)
– Charles L.
Feb 1 '16 at 20:21
2
@ErikAigner i just find that riduclous. No one reinvents stuff thats done already unless you can do better or need something specific to your program, if you inspect the code and see it does it well why bother do it yourself - not only do you waste development time and cost, you are also potentially going to bring in bugs or simply wrong implementations if you don't know completely what you are doing, these libraries are usually made people who do know what they are doing. It is not rookie to use third party libraries, its only rookie to just assume it works and not inspect the code first..
– Sir
Oct 31 '17 at 21:50
2
Oh im sorry i forgot every company ever reprograms absolutely everything, makes their own editors, operating systems, their own programming language because why use Go when you should know how to make your own right?. No one does. At the end of the day you will be using some one else's code some where unless you have no concept of time/money saving as a business. Its just totally dumb to do it twice over. Many companies use code with MIT licenses and then modify to their needs. Clearly you don't have much experience working in different companies.
– Sir
Nov 2 '17 at 19:16
|
show 5 more comments
2
@Flimzy for people who don't know what they are doing that's most likely true. Introducing unnecessary dependencies is always a bad thing.
– Erik Aigner
Sep 3 '15 at 16:25
27
@ErikAigner As long as it is 50 lines I do not have to think about, write and test, I'll take them thank you.. I have other stuff to do then reinvent the wheel.
– RickyA
Nov 2 '15 at 14:48
1
This library looks like it is not actually RFC4122 compliant: github.com/nu7hatch/gouuid/issues/28 (currently open issue as of 2/1/2016)
– Charles L.
Feb 1 '16 at 20:21
2
@ErikAigner i just find that riduclous. No one reinvents stuff thats done already unless you can do better or need something specific to your program, if you inspect the code and see it does it well why bother do it yourself - not only do you waste development time and cost, you are also potentially going to bring in bugs or simply wrong implementations if you don't know completely what you are doing, these libraries are usually made people who do know what they are doing. It is not rookie to use third party libraries, its only rookie to just assume it works and not inspect the code first..
– Sir
Oct 31 '17 at 21:50
2
Oh im sorry i forgot every company ever reprograms absolutely everything, makes their own editors, operating systems, their own programming language because why use Go when you should know how to make your own right?. No one does. At the end of the day you will be using some one else's code some where unless you have no concept of time/money saving as a business. Its just totally dumb to do it twice over. Many companies use code with MIT licenses and then modify to their needs. Clearly you don't have much experience working in different companies.
– Sir
Nov 2 '17 at 19:16
2
2
@Flimzy for people who don't know what they are doing that's most likely true. Introducing unnecessary dependencies is always a bad thing.
– Erik Aigner
Sep 3 '15 at 16:25
@Flimzy for people who don't know what they are doing that's most likely true. Introducing unnecessary dependencies is always a bad thing.
– Erik Aigner
Sep 3 '15 at 16:25
27
27
@ErikAigner As long as it is 50 lines I do not have to think about, write and test, I'll take them thank you.. I have other stuff to do then reinvent the wheel.
– RickyA
Nov 2 '15 at 14:48
@ErikAigner As long as it is 50 lines I do not have to think about, write and test, I'll take them thank you.. I have other stuff to do then reinvent the wheel.
– RickyA
Nov 2 '15 at 14:48
1
1
This library looks like it is not actually RFC4122 compliant: github.com/nu7hatch/gouuid/issues/28 (currently open issue as of 2/1/2016)
– Charles L.
Feb 1 '16 at 20:21
This library looks like it is not actually RFC4122 compliant: github.com/nu7hatch/gouuid/issues/28 (currently open issue as of 2/1/2016)
– Charles L.
Feb 1 '16 at 20:21
2
2
@ErikAigner i just find that riduclous. No one reinvents stuff thats done already unless you can do better or need something specific to your program, if you inspect the code and see it does it well why bother do it yourself - not only do you waste development time and cost, you are also potentially going to bring in bugs or simply wrong implementations if you don't know completely what you are doing, these libraries are usually made people who do know what they are doing. It is not rookie to use third party libraries, its only rookie to just assume it works and not inspect the code first..
– Sir
Oct 31 '17 at 21:50
@ErikAigner i just find that riduclous. No one reinvents stuff thats done already unless you can do better or need something specific to your program, if you inspect the code and see it does it well why bother do it yourself - not only do you waste development time and cost, you are also potentially going to bring in bugs or simply wrong implementations if you don't know completely what you are doing, these libraries are usually made people who do know what they are doing. It is not rookie to use third party libraries, its only rookie to just assume it works and not inspect the code first..
– Sir
Oct 31 '17 at 21:50
2
2
Oh im sorry i forgot every company ever reprograms absolutely everything, makes their own editors, operating systems, their own programming language because why use Go when you should know how to make your own right?. No one does. At the end of the day you will be using some one else's code some where unless you have no concept of time/money saving as a business. Its just totally dumb to do it twice over. Many companies use code with MIT licenses and then modify to their needs. Clearly you don't have much experience working in different companies.
– Sir
Nov 2 '17 at 19:16
Oh im sorry i forgot every company ever reprograms absolutely everything, makes their own editors, operating systems, their own programming language because why use Go when you should know how to make your own right?. No one does. At the end of the day you will be using some one else's code some where unless you have no concept of time/money saving as a business. Its just totally dumb to do it twice over. Many companies use code with MIT licenses and then modify to their needs. Clearly you don't have much experience working in different companies.
– Sir
Nov 2 '17 at 19:16
|
show 5 more comments
The go-uuid library is NOT RFC4122 compliant. The variant bits are not set correctly. There have been several attempts by community members to have this fixed but pull requests for the fix are not being accepted.
You can generate UUIDs using the Go uuid library I rewrote based on the go-uuid library. There are several fixes and improvements. This can be installed with:
go get github.com/twinj/uuid
You can generate random (version 4) UUIDs with:
import "github.com/twinj/uuid"
u := uuid.NewV4()
The returned UUID type is an interface and the underlying type is an array.
The library also generates v1 UUIDs and correctly generates v3 and 5 UUIDs. There are several new methods to help with printing and formatting and also new general methods to create UUIDs based off of existing data.
4
I like this package. I've officially adopted it for all my applications. I found that the nu7hatch package wasn't RFC4122-compliant.
– Richard Eng
Aug 19 '14 at 16:47
+1 Agreed, the updates and printing/formatting extensions already included.
– eduncan911
Sep 10 '14 at 16:53
4
Disclaimer missing? :p
– chakrit
Sep 22 '14 at 7:23
2
What is the library "below"? You should avoid using above and below on SO as that can change quite quickly.
– inf
Jan 18 '15 at 7:10
There's also another equivallent, satori/go.uuid. Didn't try it yet but I am going to use it as a replacement of nu7hatch dead project...
– shadyyx
Apr 7 '16 at 13:06
add a comment |
The go-uuid library is NOT RFC4122 compliant. The variant bits are not set correctly. There have been several attempts by community members to have this fixed but pull requests for the fix are not being accepted.
You can generate UUIDs using the Go uuid library I rewrote based on the go-uuid library. There are several fixes and improvements. This can be installed with:
go get github.com/twinj/uuid
You can generate random (version 4) UUIDs with:
import "github.com/twinj/uuid"
u := uuid.NewV4()
The returned UUID type is an interface and the underlying type is an array.
The library also generates v1 UUIDs and correctly generates v3 and 5 UUIDs. There are several new methods to help with printing and formatting and also new general methods to create UUIDs based off of existing data.
4
I like this package. I've officially adopted it for all my applications. I found that the nu7hatch package wasn't RFC4122-compliant.
– Richard Eng
Aug 19 '14 at 16:47
+1 Agreed, the updates and printing/formatting extensions already included.
– eduncan911
Sep 10 '14 at 16:53
4
Disclaimer missing? :p
– chakrit
Sep 22 '14 at 7:23
2
What is the library "below"? You should avoid using above and below on SO as that can change quite quickly.
– inf
Jan 18 '15 at 7:10
There's also another equivallent, satori/go.uuid. Didn't try it yet but I am going to use it as a replacement of nu7hatch dead project...
– shadyyx
Apr 7 '16 at 13:06
add a comment |
The go-uuid library is NOT RFC4122 compliant. The variant bits are not set correctly. There have been several attempts by community members to have this fixed but pull requests for the fix are not being accepted.
You can generate UUIDs using the Go uuid library I rewrote based on the go-uuid library. There are several fixes and improvements. This can be installed with:
go get github.com/twinj/uuid
You can generate random (version 4) UUIDs with:
import "github.com/twinj/uuid"
u := uuid.NewV4()
The returned UUID type is an interface and the underlying type is an array.
The library also generates v1 UUIDs and correctly generates v3 and 5 UUIDs. There are several new methods to help with printing and formatting and also new general methods to create UUIDs based off of existing data.
The go-uuid library is NOT RFC4122 compliant. The variant bits are not set correctly. There have been several attempts by community members to have this fixed but pull requests for the fix are not being accepted.
You can generate UUIDs using the Go uuid library I rewrote based on the go-uuid library. There are several fixes and improvements. This can be installed with:
go get github.com/twinj/uuid
You can generate random (version 4) UUIDs with:
import "github.com/twinj/uuid"
u := uuid.NewV4()
The returned UUID type is an interface and the underlying type is an array.
The library also generates v1 UUIDs and correctly generates v3 and 5 UUIDs. There are several new methods to help with printing and formatting and also new general methods to create UUIDs based off of existing data.
edited Jun 26 '17 at 5:07
djule5
2,11721519
2,11721519
answered Feb 17 '14 at 7:03
twinjtwinj
1,6511310
1,6511310
4
I like this package. I've officially adopted it for all my applications. I found that the nu7hatch package wasn't RFC4122-compliant.
– Richard Eng
Aug 19 '14 at 16:47
+1 Agreed, the updates and printing/formatting extensions already included.
– eduncan911
Sep 10 '14 at 16:53
4
Disclaimer missing? :p
– chakrit
Sep 22 '14 at 7:23
2
What is the library "below"? You should avoid using above and below on SO as that can change quite quickly.
– inf
Jan 18 '15 at 7:10
There's also another equivallent, satori/go.uuid. Didn't try it yet but I am going to use it as a replacement of nu7hatch dead project...
– shadyyx
Apr 7 '16 at 13:06
add a comment |
4
I like this package. I've officially adopted it for all my applications. I found that the nu7hatch package wasn't RFC4122-compliant.
– Richard Eng
Aug 19 '14 at 16:47
+1 Agreed, the updates and printing/formatting extensions already included.
– eduncan911
Sep 10 '14 at 16:53
4
Disclaimer missing? :p
– chakrit
Sep 22 '14 at 7:23
2
What is the library "below"? You should avoid using above and below on SO as that can change quite quickly.
– inf
Jan 18 '15 at 7:10
There's also another equivallent, satori/go.uuid. Didn't try it yet but I am going to use it as a replacement of nu7hatch dead project...
– shadyyx
Apr 7 '16 at 13:06
4
4
I like this package. I've officially adopted it for all my applications. I found that the nu7hatch package wasn't RFC4122-compliant.
– Richard Eng
Aug 19 '14 at 16:47
I like this package. I've officially adopted it for all my applications. I found that the nu7hatch package wasn't RFC4122-compliant.
– Richard Eng
Aug 19 '14 at 16:47
+1 Agreed, the updates and printing/formatting extensions already included.
– eduncan911
Sep 10 '14 at 16:53
+1 Agreed, the updates and printing/formatting extensions already included.
– eduncan911
Sep 10 '14 at 16:53
4
4
Disclaimer missing? :p
– chakrit
Sep 22 '14 at 7:23
Disclaimer missing? :p
– chakrit
Sep 22 '14 at 7:23
2
2
What is the library "below"? You should avoid using above and below on SO as that can change quite quickly.
– inf
Jan 18 '15 at 7:10
What is the library "below"? You should avoid using above and below on SO as that can change quite quickly.
– inf
Jan 18 '15 at 7:10
There's also another equivallent, satori/go.uuid. Didn't try it yet but I am going to use it as a replacement of nu7hatch dead project...
– shadyyx
Apr 7 '16 at 13:06
There's also another equivallent, satori/go.uuid. Didn't try it yet but I am going to use it as a replacement of nu7hatch dead project...
– shadyyx
Apr 7 '16 at 13:06
add a comment |
"crypto/rand" is cross platform pkg for random bytes generattion
package main
import (
"crypto/rand"
"fmt"
)
func pseudo_uuid() (uuid string) {
b := make(byte, 16)
_, err := rand.Read(b)
if err != nil {
fmt.Println("Error: ", err)
return
}
uuid = fmt.Sprintf("%X-%X-%X-%X-%X", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return
}
2
pseudo_uuidbecause it is missing the non-random identifiers like MAC address and whatever else RFC4122 specified? So it's actually more random.
– Xeoncross
Apr 21 '17 at 21:27
2
good answer; I've expanded it at stackoverflow.com/a/48134820/1122270, and I think a lot of folks actually don't need to use UUIDs specifically (nor the sha1/sha256 that I thought I need to use for my own random-id problem), but simply want something random and unique, and your sample provides a good start for a solution
– cnst
Jan 7 '18 at 6:26
Thanks! Simple enough
– Karl Pokus
Apr 3 at 9:32
add a comment |
"crypto/rand" is cross platform pkg for random bytes generattion
package main
import (
"crypto/rand"
"fmt"
)
func pseudo_uuid() (uuid string) {
b := make(byte, 16)
_, err := rand.Read(b)
if err != nil {
fmt.Println("Error: ", err)
return
}
uuid = fmt.Sprintf("%X-%X-%X-%X-%X", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return
}
2
pseudo_uuidbecause it is missing the non-random identifiers like MAC address and whatever else RFC4122 specified? So it's actually more random.
– Xeoncross
Apr 21 '17 at 21:27
2
good answer; I've expanded it at stackoverflow.com/a/48134820/1122270, and I think a lot of folks actually don't need to use UUIDs specifically (nor the sha1/sha256 that I thought I need to use for my own random-id problem), but simply want something random and unique, and your sample provides a good start for a solution
– cnst
Jan 7 '18 at 6:26
Thanks! Simple enough
– Karl Pokus
Apr 3 at 9:32
add a comment |
"crypto/rand" is cross platform pkg for random bytes generattion
package main
import (
"crypto/rand"
"fmt"
)
func pseudo_uuid() (uuid string) {
b := make(byte, 16)
_, err := rand.Read(b)
if err != nil {
fmt.Println("Error: ", err)
return
}
uuid = fmt.Sprintf("%X-%X-%X-%X-%X", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return
}
"crypto/rand" is cross platform pkg for random bytes generattion
package main
import (
"crypto/rand"
"fmt"
)
func pseudo_uuid() (uuid string) {
b := make(byte, 16)
_, err := rand.Read(b)
if err != nil {
fmt.Println("Error: ", err)
return
}
uuid = fmt.Sprintf("%X-%X-%X-%X-%X", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return
}
answered Sep 9 '14 at 3:12
Ken CloudKen Cloud
48744
48744
2
pseudo_uuidbecause it is missing the non-random identifiers like MAC address and whatever else RFC4122 specified? So it's actually more random.
– Xeoncross
Apr 21 '17 at 21:27
2
good answer; I've expanded it at stackoverflow.com/a/48134820/1122270, and I think a lot of folks actually don't need to use UUIDs specifically (nor the sha1/sha256 that I thought I need to use for my own random-id problem), but simply want something random and unique, and your sample provides a good start for a solution
– cnst
Jan 7 '18 at 6:26
Thanks! Simple enough
– Karl Pokus
Apr 3 at 9:32
add a comment |
2
pseudo_uuidbecause it is missing the non-random identifiers like MAC address and whatever else RFC4122 specified? So it's actually more random.
– Xeoncross
Apr 21 '17 at 21:27
2
good answer; I've expanded it at stackoverflow.com/a/48134820/1122270, and I think a lot of folks actually don't need to use UUIDs specifically (nor the sha1/sha256 that I thought I need to use for my own random-id problem), but simply want something random and unique, and your sample provides a good start for a solution
– cnst
Jan 7 '18 at 6:26
Thanks! Simple enough
– Karl Pokus
Apr 3 at 9:32
2
2
pseudo_uuid because it is missing the non-random identifiers like MAC address and whatever else RFC4122 specified? So it's actually more random.– Xeoncross
Apr 21 '17 at 21:27
pseudo_uuid because it is missing the non-random identifiers like MAC address and whatever else RFC4122 specified? So it's actually more random.– Xeoncross
Apr 21 '17 at 21:27
2
2
good answer; I've expanded it at stackoverflow.com/a/48134820/1122270, and I think a lot of folks actually don't need to use UUIDs specifically (nor the sha1/sha256 that I thought I need to use for my own random-id problem), but simply want something random and unique, and your sample provides a good start for a solution
– cnst
Jan 7 '18 at 6:26
good answer; I've expanded it at stackoverflow.com/a/48134820/1122270, and I think a lot of folks actually don't need to use UUIDs specifically (nor the sha1/sha256 that I thought I need to use for my own random-id problem), but simply want something random and unique, and your sample provides a good start for a solution
– cnst
Jan 7 '18 at 6:26
Thanks! Simple enough
– Karl Pokus
Apr 3 at 9:32
Thanks! Simple enough
– Karl Pokus
Apr 3 at 9:32
add a comment |
gofrs/uuid is the replacement for satori/go.uuid, which is the most starred UUID package for Go. It supports UUID versions 1-5 and is RFC 4122 and DCE 1.1 compliant.
import "github.com/gofrs/uuid"
// Create a Version 4 UUID, panicking on error
u := uuid.Must(uuid.NewV4())
add a comment |
gofrs/uuid is the replacement for satori/go.uuid, which is the most starred UUID package for Go. It supports UUID versions 1-5 and is RFC 4122 and DCE 1.1 compliant.
import "github.com/gofrs/uuid"
// Create a Version 4 UUID, panicking on error
u := uuid.Must(uuid.NewV4())
add a comment |
gofrs/uuid is the replacement for satori/go.uuid, which is the most starred UUID package for Go. It supports UUID versions 1-5 and is RFC 4122 and DCE 1.1 compliant.
import "github.com/gofrs/uuid"
// Create a Version 4 UUID, panicking on error
u := uuid.Must(uuid.NewV4())
gofrs/uuid is the replacement for satori/go.uuid, which is the most starred UUID package for Go. It supports UUID versions 1-5 and is RFC 4122 and DCE 1.1 compliant.
import "github.com/gofrs/uuid"
// Create a Version 4 UUID, panicking on error
u := uuid.Must(uuid.NewV4())
edited Aug 8 '18 at 17:46
answered Apr 25 '16 at 19:14
bmaupinbmaupin
7,20235158
7,20235158
add a comment |
add a comment |
From Russ Cox's post:
There's no official library. Ignoring error checking,
this seems like it would work fine:
f, _ := os.Open("/dev/urandom")
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:])
Note: In the original, pre Go 1 version the first line was:
f, _ := os.Open("/dev/urandom", os.O_RDONLY, 0)
Here it compiles and executes, only /dev/urandom returns all zeros in the playground. Should work fine locally.
In the same thread there are some other methods/references/packages found.
10
This won't generate a valid UUID though: version 4 UUIDs (the type based on random data) require a few bits to be set in a certain way to avoid conflicting with the non-random UUID formats.
– James Henstridge
Feb 28 '13 at 11:21
4
Better to useimport "crypto/rand"in my opinion, but +1 foruuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]). Combined with the OP's code, and this works great.
– chowey
May 6 '14 at 17:25
1
Using the crypto/rand package: play.golang.org/p/7JJDx4GL77. zzzz's code does what crypt/rand does, except that it also covers platforms that don't support /dev/urandom (Windows).
– Drew
Nov 4 '14 at 6:09
It should be noted that this is platform specific
– Dan Esparza
Oct 27 '16 at 18:03
1
@Matt: the problem is that the other UUID formats get their uniqueness by delegating to some other authority (e.g. that your Ethernet MAC address is unique), and then combining that with something else (e.g. the time plus a counter). If you produce a random UUID that isn't correctly formatted as a V4 one, you're weakening the system.
– James Henstridge
Sep 2 '17 at 9:45
|
show 3 more comments
From Russ Cox's post:
There's no official library. Ignoring error checking,
this seems like it would work fine:
f, _ := os.Open("/dev/urandom")
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:])
Note: In the original, pre Go 1 version the first line was:
f, _ := os.Open("/dev/urandom", os.O_RDONLY, 0)
Here it compiles and executes, only /dev/urandom returns all zeros in the playground. Should work fine locally.
In the same thread there are some other methods/references/packages found.
10
This won't generate a valid UUID though: version 4 UUIDs (the type based on random data) require a few bits to be set in a certain way to avoid conflicting with the non-random UUID formats.
– James Henstridge
Feb 28 '13 at 11:21
4
Better to useimport "crypto/rand"in my opinion, but +1 foruuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]). Combined with the OP's code, and this works great.
– chowey
May 6 '14 at 17:25
1
Using the crypto/rand package: play.golang.org/p/7JJDx4GL77. zzzz's code does what crypt/rand does, except that it also covers platforms that don't support /dev/urandom (Windows).
– Drew
Nov 4 '14 at 6:09
It should be noted that this is platform specific
– Dan Esparza
Oct 27 '16 at 18:03
1
@Matt: the problem is that the other UUID formats get their uniqueness by delegating to some other authority (e.g. that your Ethernet MAC address is unique), and then combining that with something else (e.g. the time plus a counter). If you produce a random UUID that isn't correctly formatted as a V4 one, you're weakening the system.
– James Henstridge
Sep 2 '17 at 9:45
|
show 3 more comments
From Russ Cox's post:
There's no official library. Ignoring error checking,
this seems like it would work fine:
f, _ := os.Open("/dev/urandom")
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:])
Note: In the original, pre Go 1 version the first line was:
f, _ := os.Open("/dev/urandom", os.O_RDONLY, 0)
Here it compiles and executes, only /dev/urandom returns all zeros in the playground. Should work fine locally.
In the same thread there are some other methods/references/packages found.
From Russ Cox's post:
There's no official library. Ignoring error checking,
this seems like it would work fine:
f, _ := os.Open("/dev/urandom")
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:])
Note: In the original, pre Go 1 version the first line was:
f, _ := os.Open("/dev/urandom", os.O_RDONLY, 0)
Here it compiles and executes, only /dev/urandom returns all zeros in the playground. Should work fine locally.
In the same thread there are some other methods/references/packages found.
answered Feb 28 '13 at 8:38
zzzzzzzz
55.6k12133114
55.6k12133114
10
This won't generate a valid UUID though: version 4 UUIDs (the type based on random data) require a few bits to be set in a certain way to avoid conflicting with the non-random UUID formats.
– James Henstridge
Feb 28 '13 at 11:21
4
Better to useimport "crypto/rand"in my opinion, but +1 foruuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]). Combined with the OP's code, and this works great.
– chowey
May 6 '14 at 17:25
1
Using the crypto/rand package: play.golang.org/p/7JJDx4GL77. zzzz's code does what crypt/rand does, except that it also covers platforms that don't support /dev/urandom (Windows).
– Drew
Nov 4 '14 at 6:09
It should be noted that this is platform specific
– Dan Esparza
Oct 27 '16 at 18:03
1
@Matt: the problem is that the other UUID formats get their uniqueness by delegating to some other authority (e.g. that your Ethernet MAC address is unique), and then combining that with something else (e.g. the time plus a counter). If you produce a random UUID that isn't correctly formatted as a V4 one, you're weakening the system.
– James Henstridge
Sep 2 '17 at 9:45
|
show 3 more comments
10
This won't generate a valid UUID though: version 4 UUIDs (the type based on random data) require a few bits to be set in a certain way to avoid conflicting with the non-random UUID formats.
– James Henstridge
Feb 28 '13 at 11:21
4
Better to useimport "crypto/rand"in my opinion, but +1 foruuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]). Combined with the OP's code, and this works great.
– chowey
May 6 '14 at 17:25
1
Using the crypto/rand package: play.golang.org/p/7JJDx4GL77. zzzz's code does what crypt/rand does, except that it also covers platforms that don't support /dev/urandom (Windows).
– Drew
Nov 4 '14 at 6:09
It should be noted that this is platform specific
– Dan Esparza
Oct 27 '16 at 18:03
1
@Matt: the problem is that the other UUID formats get their uniqueness by delegating to some other authority (e.g. that your Ethernet MAC address is unique), and then combining that with something else (e.g. the time plus a counter). If you produce a random UUID that isn't correctly formatted as a V4 one, you're weakening the system.
– James Henstridge
Sep 2 '17 at 9:45
10
10
This won't generate a valid UUID though: version 4 UUIDs (the type based on random data) require a few bits to be set in a certain way to avoid conflicting with the non-random UUID formats.
– James Henstridge
Feb 28 '13 at 11:21
This won't generate a valid UUID though: version 4 UUIDs (the type based on random data) require a few bits to be set in a certain way to avoid conflicting with the non-random UUID formats.
– James Henstridge
Feb 28 '13 at 11:21
4
4
Better to use
import "crypto/rand" in my opinion, but +1 for uuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]). Combined with the OP's code, and this works great.– chowey
May 6 '14 at 17:25
Better to use
import "crypto/rand" in my opinion, but +1 for uuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]). Combined with the OP's code, and this works great.– chowey
May 6 '14 at 17:25
1
1
Using the crypto/rand package: play.golang.org/p/7JJDx4GL77. zzzz's code does what crypt/rand does, except that it also covers platforms that don't support /dev/urandom (Windows).
– Drew
Nov 4 '14 at 6:09
Using the crypto/rand package: play.golang.org/p/7JJDx4GL77. zzzz's code does what crypt/rand does, except that it also covers platforms that don't support /dev/urandom (Windows).
– Drew
Nov 4 '14 at 6:09
It should be noted that this is platform specific
– Dan Esparza
Oct 27 '16 at 18:03
It should be noted that this is platform specific
– Dan Esparza
Oct 27 '16 at 18:03
1
1
@Matt: the problem is that the other UUID formats get their uniqueness by delegating to some other authority (e.g. that your Ethernet MAC address is unique), and then combining that with something else (e.g. the time plus a counter). If you produce a random UUID that isn't correctly formatted as a V4 one, you're weakening the system.
– James Henstridge
Sep 2 '17 at 9:45
@Matt: the problem is that the other UUID formats get their uniqueness by delegating to some other authority (e.g. that your Ethernet MAC address is unique), and then combining that with something else (e.g. the time plus a counter). If you produce a random UUID that isn't correctly formatted as a V4 one, you're weakening the system.
– James Henstridge
Sep 2 '17 at 9:45
|
show 3 more comments
As part of the uuid spec, if you generate a uuid from random it must contain a "4" as the 13th character and a "8", "9", "a", or "b" in the 17th (source).
// this makes sure that the 13th character is "4"
u[6] = (u[6] | 0x40) & 0x4F
// this makes sure that the 17th is "8", "9", "a", or "b"
u[8] = (u[8] | 0x80) & 0xBF
add a comment |
As part of the uuid spec, if you generate a uuid from random it must contain a "4" as the 13th character and a "8", "9", "a", or "b" in the 17th (source).
// this makes sure that the 13th character is "4"
u[6] = (u[6] | 0x40) & 0x4F
// this makes sure that the 17th is "8", "9", "a", or "b"
u[8] = (u[8] | 0x80) & 0xBF
add a comment |
As part of the uuid spec, if you generate a uuid from random it must contain a "4" as the 13th character and a "8", "9", "a", or "b" in the 17th (source).
// this makes sure that the 13th character is "4"
u[6] = (u[6] | 0x40) & 0x4F
// this makes sure that the 17th is "8", "9", "a", or "b"
u[8] = (u[8] | 0x80) & 0xBF
As part of the uuid spec, if you generate a uuid from random it must contain a "4" as the 13th character and a "8", "9", "a", or "b" in the 17th (source).
// this makes sure that the 13th character is "4"
u[6] = (u[6] | 0x40) & 0x4F
// this makes sure that the 17th is "8", "9", "a", or "b"
u[8] = (u[8] | 0x80) & 0xBF
edited Nov 22 '18 at 20:32
answered Jan 6 '15 at 5:57
eric chiangeric chiang
1,2681418
1,2681418
add a comment |
add a comment |
An official implementation by Google is currently under development but not stable yet: https://github.com/google/uuid
Looks like it's the continuation of https://github.com/pborman/uuid from this answer
Generating a version 4 UUID works like this:
uuid, err := uuid.NewRandom()
Update:
A version 1.0.0 has been released and the note regarding possible instability has been removed.
add a comment |
An official implementation by Google is currently under development but not stable yet: https://github.com/google/uuid
Looks like it's the continuation of https://github.com/pborman/uuid from this answer
Generating a version 4 UUID works like this:
uuid, err := uuid.NewRandom()
Update:
A version 1.0.0 has been released and the note regarding possible instability has been removed.
add a comment |
An official implementation by Google is currently under development but not stable yet: https://github.com/google/uuid
Looks like it's the continuation of https://github.com/pborman/uuid from this answer
Generating a version 4 UUID works like this:
uuid, err := uuid.NewRandom()
Update:
A version 1.0.0 has been released and the note regarding possible instability has been removed.
An official implementation by Google is currently under development but not stable yet: https://github.com/google/uuid
Looks like it's the continuation of https://github.com/pborman/uuid from this answer
Generating a version 4 UUID works like this:
uuid, err := uuid.NewRandom()
Update:
A version 1.0.0 has been released and the note regarding possible instability has been removed.
edited Dec 6 '18 at 14:10
answered Nov 22 '17 at 11:49
shutefanshutefan
1,58241521
1,58241521
add a comment |
add a comment |
The gorand package has a UUID method that returns a Version 4 (randomly generated) UUID in its canonical string representation ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") and it's RFC 4122 compliant.
It also uses the crypto/rand package to ensure the most cryptographically secure generation of UUIDs across all platforms supported by Go.
import "github.com/leonelquinteros/gorand"
func main() {
uuid, err := gorand.UUID()
if err != nil {
panic(err.Error())
}
println(uuid)
}
add a comment |
The gorand package has a UUID method that returns a Version 4 (randomly generated) UUID in its canonical string representation ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") and it's RFC 4122 compliant.
It also uses the crypto/rand package to ensure the most cryptographically secure generation of UUIDs across all platforms supported by Go.
import "github.com/leonelquinteros/gorand"
func main() {
uuid, err := gorand.UUID()
if err != nil {
panic(err.Error())
}
println(uuid)
}
add a comment |
The gorand package has a UUID method that returns a Version 4 (randomly generated) UUID in its canonical string representation ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") and it's RFC 4122 compliant.
It also uses the crypto/rand package to ensure the most cryptographically secure generation of UUIDs across all platforms supported by Go.
import "github.com/leonelquinteros/gorand"
func main() {
uuid, err := gorand.UUID()
if err != nil {
panic(err.Error())
}
println(uuid)
}
The gorand package has a UUID method that returns a Version 4 (randomly generated) UUID in its canonical string representation ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") and it's RFC 4122 compliant.
It also uses the crypto/rand package to ensure the most cryptographically secure generation of UUIDs across all platforms supported by Go.
import "github.com/leonelquinteros/gorand"
func main() {
uuid, err := gorand.UUID()
if err != nil {
panic(err.Error())
}
println(uuid)
}
edited Jun 27 '16 at 14:12
answered Jun 15 '16 at 0:00
peiiionpeiiion
1904
1904
add a comment |
add a comment |
You should use google/uuid https://github.com/google/uuid
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
id, err := uuid.NewUUID()
if err !=nil {
// handle error
}
fmt.Printf(id.String())
}
This package is RFC4122 and DCE 1.1 compliant
2
I believe this answer should be on the top since this seems the most correct solution for current time. Accepted answer does not have the most upvotes and top 2 upvoted answers mention packages which might not be better thangoogle/uuid
– ViKiG
Apr 4 at 14:27
add a comment |
You should use google/uuid https://github.com/google/uuid
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
id, err := uuid.NewUUID()
if err !=nil {
// handle error
}
fmt.Printf(id.String())
}
This package is RFC4122 and DCE 1.1 compliant
2
I believe this answer should be on the top since this seems the most correct solution for current time. Accepted answer does not have the most upvotes and top 2 upvoted answers mention packages which might not be better thangoogle/uuid
– ViKiG
Apr 4 at 14:27
add a comment |
You should use google/uuid https://github.com/google/uuid
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
id, err := uuid.NewUUID()
if err !=nil {
// handle error
}
fmt.Printf(id.String())
}
This package is RFC4122 and DCE 1.1 compliant
You should use google/uuid https://github.com/google/uuid
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
id, err := uuid.NewUUID()
if err !=nil {
// handle error
}
fmt.Printf(id.String())
}
This package is RFC4122 and DCE 1.1 compliant
edited Apr 5 at 10:45
answered Mar 29 at 10:45
stwykdstwykd
13217
13217
2
I believe this answer should be on the top since this seems the most correct solution for current time. Accepted answer does not have the most upvotes and top 2 upvoted answers mention packages which might not be better thangoogle/uuid
– ViKiG
Apr 4 at 14:27
add a comment |
2
I believe this answer should be on the top since this seems the most correct solution for current time. Accepted answer does not have the most upvotes and top 2 upvoted answers mention packages which might not be better thangoogle/uuid
– ViKiG
Apr 4 at 14:27
2
2
I believe this answer should be on the top since this seems the most correct solution for current time. Accepted answer does not have the most upvotes and top 2 upvoted answers mention packages which might not be better than
google/uuid– ViKiG
Apr 4 at 14:27
I believe this answer should be on the top since this seems the most correct solution for current time. Accepted answer does not have the most upvotes and top 2 upvoted answers mention packages which might not be better than
google/uuid– ViKiG
Apr 4 at 14:27
add a comment |
On Linux, you can read from /proc/sys/kernel/random/uuid:
package main
import "io/ioutil"
import "fmt"
func main() {
u, _ := ioutil.ReadFile("/proc/sys/kernel/random/uuid")
fmt.Println(string(u))
}
No external dependencies!
$ go run uuid.go
3ee995e3-0c96-4e30-ac1e-f7f04fd03e44
1
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:08
add a comment |
On Linux, you can read from /proc/sys/kernel/random/uuid:
package main
import "io/ioutil"
import "fmt"
func main() {
u, _ := ioutil.ReadFile("/proc/sys/kernel/random/uuid")
fmt.Println(string(u))
}
No external dependencies!
$ go run uuid.go
3ee995e3-0c96-4e30-ac1e-f7f04fd03e44
1
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:08
add a comment |
On Linux, you can read from /proc/sys/kernel/random/uuid:
package main
import "io/ioutil"
import "fmt"
func main() {
u, _ := ioutil.ReadFile("/proc/sys/kernel/random/uuid")
fmt.Println(string(u))
}
No external dependencies!
$ go run uuid.go
3ee995e3-0c96-4e30-ac1e-f7f04fd03e44
On Linux, you can read from /proc/sys/kernel/random/uuid:
package main
import "io/ioutil"
import "fmt"
func main() {
u, _ := ioutil.ReadFile("/proc/sys/kernel/random/uuid")
fmt.Println(string(u))
}
No external dependencies!
$ go run uuid.go
3ee995e3-0c96-4e30-ac1e-f7f04fd03e44
answered Nov 8 '17 at 3:59
A.J.A.J.
19415
19415
1
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:08
add a comment |
1
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:08
1
1
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:08
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:08
add a comment |
This library is our standard for uuid generation and parsing:
https://github.com/pborman/uuid
add a comment |
This library is our standard for uuid generation and parsing:
https://github.com/pborman/uuid
add a comment |
This library is our standard for uuid generation and parsing:
https://github.com/pborman/uuid
This library is our standard for uuid generation and parsing:
https://github.com/pborman/uuid
answered Aug 14 '15 at 18:10
James McGillJames McGill
943
943
add a comment |
add a comment |
For Windows, I did recently this:
// +build windows
package main
import (
"syscall"
"unsafe"
)
var (
modrpcrt4 = syscall.NewLazyDLL("rpcrt4.dll")
procUuidCreate = modrpcrt4.NewProc("UuidCreate")
)
const (
RPC_S_OK = 0
)
func NewUuid() (byte, error) {
var uuid [16]byte
rc, _, e := syscall.Syscall(procUuidCreate.Addr(), 1,
uintptr(unsafe.Pointer(&uuid[0])), 0, 0)
if int(rc) != RPC_S_OK {
if e != 0 {
return nil, error(e)
} else {
return nil, syscall.EINVAL
}
}
return uuid[:], nil
}
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:10
@Byebye, I wonder why you consider yourself an authority to decide what "is worse" (and what is not) to skim through all the answers given to this question and downvote all which are "system-dependent"? These answers were given to a) widen the horizon of all possible choices, and b) collectively present a complete picture. So please stop childishly "playing SO" and put some thought before you action.
– kostix
Aug 7 '18 at 8:50
Short answer. Writing maintainable code. Your answer cannot be ported to a different platform. So if the OP would choose to move their application to another platform the application would break. I've had my fair share of people who wrote platform dependent code where it is totally unnecessary and it creates more trouble then it's worth. You don't write code for just yourself. You write code for the people who will maintain it after you're gone. That's why this answer is not appropriate. No reason to resort to ad hominems and call me childish.
– Byebye
Aug 7 '18 at 9:12
1
@Byebye, I overreacted, so please excuse me for the attack. I'm not convinced of your reasons, still, but supposedly it's that "let's agree to disagree" case.
– kostix
Aug 7 '18 at 9:22
add a comment |
For Windows, I did recently this:
// +build windows
package main
import (
"syscall"
"unsafe"
)
var (
modrpcrt4 = syscall.NewLazyDLL("rpcrt4.dll")
procUuidCreate = modrpcrt4.NewProc("UuidCreate")
)
const (
RPC_S_OK = 0
)
func NewUuid() (byte, error) {
var uuid [16]byte
rc, _, e := syscall.Syscall(procUuidCreate.Addr(), 1,
uintptr(unsafe.Pointer(&uuid[0])), 0, 0)
if int(rc) != RPC_S_OK {
if e != 0 {
return nil, error(e)
} else {
return nil, syscall.EINVAL
}
}
return uuid[:], nil
}
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:10
@Byebye, I wonder why you consider yourself an authority to decide what "is worse" (and what is not) to skim through all the answers given to this question and downvote all which are "system-dependent"? These answers were given to a) widen the horizon of all possible choices, and b) collectively present a complete picture. So please stop childishly "playing SO" and put some thought before you action.
– kostix
Aug 7 '18 at 8:50
Short answer. Writing maintainable code. Your answer cannot be ported to a different platform. So if the OP would choose to move their application to another platform the application would break. I've had my fair share of people who wrote platform dependent code where it is totally unnecessary and it creates more trouble then it's worth. You don't write code for just yourself. You write code for the people who will maintain it after you're gone. That's why this answer is not appropriate. No reason to resort to ad hominems and call me childish.
– Byebye
Aug 7 '18 at 9:12
1
@Byebye, I overreacted, so please excuse me for the attack. I'm not convinced of your reasons, still, but supposedly it's that "let's agree to disagree" case.
– kostix
Aug 7 '18 at 9:22
add a comment |
For Windows, I did recently this:
// +build windows
package main
import (
"syscall"
"unsafe"
)
var (
modrpcrt4 = syscall.NewLazyDLL("rpcrt4.dll")
procUuidCreate = modrpcrt4.NewProc("UuidCreate")
)
const (
RPC_S_OK = 0
)
func NewUuid() (byte, error) {
var uuid [16]byte
rc, _, e := syscall.Syscall(procUuidCreate.Addr(), 1,
uintptr(unsafe.Pointer(&uuid[0])), 0, 0)
if int(rc) != RPC_S_OK {
if e != 0 {
return nil, error(e)
} else {
return nil, syscall.EINVAL
}
}
return uuid[:], nil
}
For Windows, I did recently this:
// +build windows
package main
import (
"syscall"
"unsafe"
)
var (
modrpcrt4 = syscall.NewLazyDLL("rpcrt4.dll")
procUuidCreate = modrpcrt4.NewProc("UuidCreate")
)
const (
RPC_S_OK = 0
)
func NewUuid() (byte, error) {
var uuid [16]byte
rc, _, e := syscall.Syscall(procUuidCreate.Addr(), 1,
uintptr(unsafe.Pointer(&uuid[0])), 0, 0)
if int(rc) != RPC_S_OK {
if e != 0 {
return nil, error(e)
} else {
return nil, syscall.EINVAL
}
}
return uuid[:], nil
}
answered Feb 28 '13 at 9:22
kostixkostix
31.6k649106
31.6k649106
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:10
@Byebye, I wonder why you consider yourself an authority to decide what "is worse" (and what is not) to skim through all the answers given to this question and downvote all which are "system-dependent"? These answers were given to a) widen the horizon of all possible choices, and b) collectively present a complete picture. So please stop childishly "playing SO" and put some thought before you action.
– kostix
Aug 7 '18 at 8:50
Short answer. Writing maintainable code. Your answer cannot be ported to a different platform. So if the OP would choose to move their application to another platform the application would break. I've had my fair share of people who wrote platform dependent code where it is totally unnecessary and it creates more trouble then it's worth. You don't write code for just yourself. You write code for the people who will maintain it after you're gone. That's why this answer is not appropriate. No reason to resort to ad hominems and call me childish.
– Byebye
Aug 7 '18 at 9:12
1
@Byebye, I overreacted, so please excuse me for the attack. I'm not convinced of your reasons, still, but supposedly it's that "let's agree to disagree" case.
– kostix
Aug 7 '18 at 9:22
add a comment |
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:10
@Byebye, I wonder why you consider yourself an authority to decide what "is worse" (and what is not) to skim through all the answers given to this question and downvote all which are "system-dependent"? These answers were given to a) widen the horizon of all possible choices, and b) collectively present a complete picture. So please stop childishly "playing SO" and put some thought before you action.
– kostix
Aug 7 '18 at 8:50
Short answer. Writing maintainable code. Your answer cannot be ported to a different platform. So if the OP would choose to move their application to another platform the application would break. I've had my fair share of people who wrote platform dependent code where it is totally unnecessary and it creates more trouble then it's worth. You don't write code for just yourself. You write code for the people who will maintain it after you're gone. That's why this answer is not appropriate. No reason to resort to ad hominems and call me childish.
– Byebye
Aug 7 '18 at 9:12
1
@Byebye, I overreacted, so please excuse me for the attack. I'm not convinced of your reasons, still, but supposedly it's that "let's agree to disagree" case.
– kostix
Aug 7 '18 at 9:22
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:10
Downvoted because directly depending on a host-platform in a programming language that's used for multi-platform applications is worse than an external dependency.
– Byebye
Aug 7 '18 at 8:10
@Byebye, I wonder why you consider yourself an authority to decide what "is worse" (and what is not) to skim through all the answers given to this question and downvote all which are "system-dependent"? These answers were given to a) widen the horizon of all possible choices, and b) collectively present a complete picture. So please stop childishly "playing SO" and put some thought before you action.
– kostix
Aug 7 '18 at 8:50
@Byebye, I wonder why you consider yourself an authority to decide what "is worse" (and what is not) to skim through all the answers given to this question and downvote all which are "system-dependent"? These answers were given to a) widen the horizon of all possible choices, and b) collectively present a complete picture. So please stop childishly "playing SO" and put some thought before you action.
– kostix
Aug 7 '18 at 8:50
Short answer. Writing maintainable code. Your answer cannot be ported to a different platform. So if the OP would choose to move their application to another platform the application would break. I've had my fair share of people who wrote platform dependent code where it is totally unnecessary and it creates more trouble then it's worth. You don't write code for just yourself. You write code for the people who will maintain it after you're gone. That's why this answer is not appropriate. No reason to resort to ad hominems and call me childish.
– Byebye
Aug 7 '18 at 9:12
Short answer. Writing maintainable code. Your answer cannot be ported to a different platform. So if the OP would choose to move their application to another platform the application would break. I've had my fair share of people who wrote platform dependent code where it is totally unnecessary and it creates more trouble then it's worth. You don't write code for just yourself. You write code for the people who will maintain it after you're gone. That's why this answer is not appropriate. No reason to resort to ad hominems and call me childish.
– Byebye
Aug 7 '18 at 9:12
1
1
@Byebye, I overreacted, so please excuse me for the attack. I'm not convinced of your reasons, still, but supposedly it's that "let's agree to disagree" case.
– kostix
Aug 7 '18 at 9:22
@Byebye, I overreacted, so please excuse me for the attack. I'm not convinced of your reasons, still, but supposedly it's that "let's agree to disagree" case.
– kostix
Aug 7 '18 at 9:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15130321%2fis-there-a-method-to-generate-a-uuid-with-go-language%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This answer seems more apt now.
– ViKiG
Apr 4 at 14:29