Generating a unique number or string


#1

My shortcodes often need unique identifiers for scripting and WAI-ARIA purposes. I have cheated by base64ing the {{.Inner}} and appending it like id="thing-{{ $uniq }}".

I’m assuming there’s a better / standard way?

Many thanks.


#2
{{ now.UnixNano }}

Is probably the closest … You also have the related shuffle func.


#3

Thank you, @bep. I’ll work with that. Was looking at shuffle, but since this is a small, one-line shortcode UnixNano seems like a good fit.


#4

Hashing something, e.g. .Inner, with either md5, sha1 or sha256 could work as well. But now.UnixNano might more performant.


#5

I was using those before, yeah, but they only work if each instance has different content. Which is likely but not 100% dependable.