google / uuid

Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
BSD 3-Clause "New" or "Revised" License
5.26k stars 362 forks source link

Zero Value Check #45

Closed jrefior closed 5 years ago

jrefior commented 5 years ago

I can write a convenience function to check for the zero value. For example:


package main

import (
    "fmt"

    "github.com/google/uuid"
)

func IsZero(id uuid.UUID) bool {
    for x := 0; x < 16; x++ {
        if id[x] != 0 {
            return false
        }
    }
    return true
}

func main() {
    id := uuid.UUID{}
    fmt.Printf("is %v zero? %t\n", id, IsZero(id))
    id, _ = uuid.NewRandom()
    fmt.Printf("is %v zero? %t\n", id, IsZero(id))
}

Is that the thing to do, or is there a better way to check for the zero value?

ferhatelmas commented 5 years ago

yes, just test id == uuid.Nil

jrefior commented 5 years ago

Ah thank you, I missed that in the Variables section of GoDoc.