Skip to content

Instantly share code, notes, and snippets.

@napsy
Created March 12, 2011 08:25
Show Gist options
  • Save napsy/867132 to your computer and use it in GitHub Desktop.
Save napsy/867132 to your computer and use it in GitHub Desktop.
package uuid
// #include <stdlib.h>
// #include <uuid/uuid.h>
import "C"
import "unsafe"
func Generate() []byte {
var uuid []byte = make([]byte, 16)
uuid_ := C.malloc(16)
C.uuid_generate((*C.uchar)(unsafe.Pointer(uuid_)))
ptr := (*[16]byte)(unsafe.Pointer(uuid_))
copy(uuid, ptr[:])
C.free(uuid_)
return uuid
}
func Unparse(uuid []byte) string {
var uuid_c *C.uchar
uuid_c = (*C.uchar)(unsafe.Pointer(&uuid[0]))
ptr := C.malloc(37)
uuid_c_str := (*C.char)(unsafe.Pointer(ptr))
C.uuid_unparse(uuid_c, uuid_c_str)
uuid_str := C.GoString(uuid_c_str)
C.free(ptr)
return uuid_str
}
func Parse(uuid_str string) []byte {
var uuid []byte = make([]byte, 16)
ptr := C.malloc(16)
uuid_c_str := C.CString(uuid_str)
C.uuid_parse(uuid_c_str, (*C.uchar)(unsafe.Pointer(ptr)))
C.free(unsafe.Pointer(uuid_c_str))
ptr1 := (*[16]byte)(unsafe.Pointer(ptr))
copy(uuid, ptr1[:])
C.free(ptr)
return uuid
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment