The os.Stat function in Hugo


hugo

The os.Stat function in Hugo (see the docs) is useful for getting info on a file.

For example, say I wanted to get the info on this page. The following shortcode:

{{ $file := .Get 0 }}
{{ $stat := os.Stat $file }}

<p>
  <strong>Name</strong><br>
  <code>{{ $stat.Name }}</code>
</p>
<p>
  <strong>Size</strong><br>
  <code>{{ $stat.Size }}</code> bytes
</p>
<p>
  <strong>Mode</strong><br>
  <code>{{ $stat.Mode }}</code>
</p>
<p>
  <strong>ModTime</strong><br>
  <code>{{ $stat.ModTime }}</code>
</p>
<p>
  <strong>IsDir</strong><br>
  <code>{{ $stat.IsDir }}</code>
</p>

Would give this output:

Name
the-os-stat-function-in-hugo.md

Size
846 bytes

Mode
-rw-r--r--

ModTime
2019-01-21 19:40:49.701262856 +0000 UTC

IsDir
false