HUGO

now.Unix behaviour

I’m trying to use now.Unix to create unique identifiers in a shortcode, but repeated calls to the shortcode returns the exact same time. It seems like now.Unix is evaluated once per build and then reused on subsequent calls. Is this true?

now.Unix is seconds since 1970-01-01 00:00:00 UTC.

Try now.UnixNano.

With 5 instances of the same shortcode on a page:

now.Unix = 1636152179
now.UnixNano = 1636152179739822286

now.Unix = 1636152179
now.UnixNano = 1636152179739835281

now.Unix = 1636152179
now.UnixNano = 1636152179739846523

now.Unix = 1636152179
now.UnixNano = 1636152179739856848

now.Unix = 1636152179
now.UnixNano = 1636152179739866681

You can also use the .Ordinal to get a unique id of each shortcode on a given page.

I see. Thank you.
I guess I didn’t look to hard at the outputted number, otherwise I would have court that myself. The docs for the Unix command is a little confusing. It says: “.Unix returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC.” Maybe I’m just reading it wrong, but I assumed that it would return a number to nearest nano second.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.

I agree. See https://github.com/gohugoio/hugoDocs/pull/1575.