Problem with os.Stat

Why is this not working:
Last written the: {{ (os.Stat .File).ModTime }}
It complains with ".File.Path on zero object. wrap it in if or with: {{with .File}}{{ .Path}}{{end}}
with other similar codes he says that he can’t access a proper path.

1) The argument to os.Stat is a path, not a file object. See docs.


{{ (os.Stat .File).ModTime }}


{{ (os.Stat .File.Path).ModTime }}

2) If this code is rendered on every page, Hugo will throw an error because not every page is backed by a file. So you need to code defensively:

{{ with .File }}
  {{ (os.Stat .Path).ModTime }}
{{ end }}

3) In regard to the .File.Path on zero object message, I’d need to see more.

1 Like

Thanks, I had not tried the 2) despite hugo’s warning to do so, because superficially it seemed to me nonsensical that .File wouldn’t be accessible from all pages.

For most sites, the majority of taxonomy and term pages will not be backed by a file.

And if you don’t create files for your top level sections, Hugo generates a list page for you, but there’s no file behind it.

1 Like

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