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.33k stars 369 forks source link

Typos and why does New panic? #12

Closed corburn closed 6 years ago

corburn commented 7 years ago

Typos

'from' is repeated twice in this comment:

https://godoc.org/github.com/google/uuid#NewRandom

A note about uniqueness derived from from the UUID Wikipedia entry:

NewRandom() returns an error, not a panic:

https://godoc.org/github.com/google/uuid#NewRandom

NewRandom returns a Random (Version 4) UUID or panics.

Why does New panic?

It is surprising New() panics when compared to the html/template package, for example, where functions that panic include the word Must:

var t = template.Must(template.New("name").Parse("html"))

https://godoc.org/github.com/google/uuid#New

New ~is~ creates a new random UUID or panics. New is equivalent to the expression

TheDahv commented 7 years ago

Just came to file this same bug. I am rewriting some code to unpack over the New panic potential, and decided to detect and find errors myself. Go over to the documentation for NewRandom and the documentation says it panics, but the signature and the code itself implies that it returns an error I can manage myself.

martinlindhe commented 6 years ago

First typo was addressed in https://github.com/google/uuid/commit/0614758b5f5e81f4e7d87184b9d97fa4385f2842

second typo is being addressed in https://github.com/google/uuid/pull/27/commits/bb91e0b0e28d478ae15f8caad3ab9fa6fd48ab4e

solarfly73 commented 6 years ago

New() calling panic is deceptive. The developer should have control over calling Must(NewRandom()) directly (since changing New() to return an error would be a breaking change for anyone using the library to date). A New() that fails should set the UUID to all 0, and the UUID can be validated with a secondary function IsValid() or something stupid.

pborman commented 6 years ago

New is often used as a global:

var instance = uuid.New()

New should never fail unless your random number generated is broken. A bad random number generator is probably worth panicing over.

pborman commented 6 years ago

I am closing this out as I don't think this will change.