Creating uuid from byte array

Hello there, please excuse the stupidity of my question.
I’m trying to switch from Java to Go and I was wondering if there is a way to create a UUID from a UTF-8 byte array of a string.

For example this is how I’d do it in Java:
UUID.nameUUIDFromBytes((string).getBytes(Charsets.UTF_8));

I’ve tried the following Code and googled around for awhile but to no avail:
bytes := make([]byte, 16) strings.NewReader(string).Read(bytes) print(fmt.Sprintf("%x-%x-%x-%x-%x", bytes[0:4], bytes[4:6], bytes[6:8], bytes[8:10], bytes[10:]))

But the UUID that it creates is much longer than in Java and is completely different characters.
Any help would be appreciated and again sorry if my question is stupidly simple to answer.

I use this package for UUID generation: http://godoc.org/github.com/satori/go.uuid#FromBytes and it has many functions including the from byte slice function I have linked here. I hope this helps you.

1 Like

Thanks for the speedy reply, but I still seem to be having an issue. When I use the string "testing#123" I get two different UUIDs.

Java: 7bde83ed-710d-3304-b813-c89589f05b09
Go: 74657374-696e-6723-3132-330000000000

testing#123 isn’t a valid representation of any sort of UUID so I’m guessing there’s no standard to lean on here. The Go packages seams to just use the bytes straight up. The Java package does something else, not clear at all to me what by just looking at the UUID.

I think this is more what you might be looking for: https://www.slintes.net/2017/01/29/Generating-a-namespace-name-UUID-in-Go-and-Java/

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.