3 ms·
My Go is rusty, but this look a little error prone: // Note that this will create a new UUID each time its .String() method // is invoked. fmt.Prin
by swah 11y ago
My Go is rusty, but this look a little error prone:
// Note that this will create a new UUID each time its .String() method
// is invoked.
fmt.Printf("%s", u) // Cekw67uyMpBGZLRP2HFVbe
fmt.Printf("%s", u) // 4pUYNRFHTG3YVgThPZvCgC
Just imagine an user using u directly instead of assigning to a var:
token := "secret" + u
othertoken := "app" + u
- ocharles 11y agoYes, this is a terrible idea - it violates referential transparency, which is a property that is always worth working towards, imo. I can't think of a reason to justify breaking that property in this scenario.
- vox_mollis 11y agoThat can be mostly avoided by naming things correctly. uuid_generator would be far less likely to be misused than u. For whatever reason, however, descriptive variables are not very popular in the Go community.
- LukeShu 11y agoSure, but .String() wouldn't be a good method for uuid_generator to use to generate a new UUID--String() should be a string representation of the thing; it says "this is a string of me". Which is where the confusion would come from.
- Spiritus 11y agoAuthor here. Yes, this is indeed a terrible idea! Will definitely fix it.
- Spiritus 11y agoFixed in https://github.com/renstrom/shortuuid/commit/847debec9c7c091fdb85d26bfe9111c0be3dca08 https://github.com/renstrom/shortuuid/commit/847debec9c7c091...