Is embed.FS not expected to be walkable by fs.WalkDir()?

457 views
Skip to first unread message

John

unread,
Jun 21, 2021, 1:42:23 AM6/21/21
to golang-nuts
package main

import (
"embed"
"io/fs"
"log"
)

//go:embed somefile.txt
var FS embed.FS

func main() {
  err := fs.WalkDir(
    FS,
    "",
    func(path string, d fs.DirEntry, err error) error {
      log.Println("path: ", path)
      return nil
    },
  )

  if err != nil {
    log.Println("err: ", err)
  }
}

This will output a single empty long line:

2021/06/20 22:32:29 path:  

d comes set to nil, which seems to be a valid condition according to the docs:

First, if the initial fs.Stat on the root directory fails, WalkDir calls the function with path set to root, d set to nil, and err set to the error from fs.Stat.

This seems like an undesirable outcome to be unable to walk the embed.FS, but I figure maybe it doesn't do so to only allow absolute paths for speed?




Axel Wagner

unread,
Jun 21, 2021, 2:53:03 AM6/21/21
to John, golang-nuts
You need to pass "." to `fs.WalkDir`, not "".

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1b00707c-9f4e-45de-87a8-0cbdda1e2fd4n%40googlegroups.com.

John

unread,
Jun 21, 2021, 3:29:43 AM6/21/21
to golang-nuts

Thanks for that axel.  I had tried "./" and "/".  I guess I missed one :)
Reply all
Reply to author
Forward
0 new messages