Search code examples
gotypesuuid

Initialize custom uuid.UUID type in Golang


In Golang it is valid to create a type:

type RoleID uuid.UUID

n1 := RoleID(uuid.New())

fmt.Println("n: ", n1)

does not seem to work. I want

n: 35ae88a1-72cd-4116-9d32-b9762ecb51b4

output

n:  [12 86 96 165 221 15 69 229 148 188 84 87 1 177 30 67]

Solution

  • Use .String() for the format you want:

    type RoleID = uuid.UUID
    
    n1 := RoleID(uuid.New())
    
    fmt.Println("n: ", n1.String())