Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot ReadFile when using embed.FS on a directory #45070

Closed
bravecorvus opened this issue Mar 16, 2021 · 4 comments
Closed

Cannot ReadFile when using embed.FS on a directory #45070

bravecorvus opened this issue Mar 16, 2021 · 4 comments

Comments

@bravecorvus
Copy link

bravecorvus commented Mar 16, 2021

What version of Go are you using (go version)?

$ go version go1.16.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/Users/andrewlee/go/bin"
GOCACHE="/Users/andrewlee/Library/Caches/go-build"
GOENV="/Users/andrewlee/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/andrewlee/go/pkg/mod"
GONOPROXY="*.amazonaws.com/v1/repos"
GONOSUMDB="*.amazonaws.com/v1/repos"
GOOS="darwin"
GOPATH="/Users/andrewlee/go"
GOPRIVATE="*.amazonaws.com/v1/repos"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.16.2/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16.2/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="0"
GOMOD="/Users/andrewlee/Projects/TestProjects/test_bindata/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/3k/x98sh83s31nb6kl86555q0c80000gn/T/go-build307906134=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Created a simple example to see how I can replace https://github.com/gilgameshskytrooper/go-bindata with native 1.16 file embed functionality.

root/
├── files/
│   ├── hello.txt
└── main.go
└── go.mod

hello.txt

Hello World

go.mod

module test_bindata

go 1.16

main.go

package main

import (
	"embed"
	_ "embed"
	"fmt"
)

// Also tried "files"
// go:embed files/*
var embedFS embed.FS

func main() {
        // Also tried "hello.txt", "/files/hello.txt", "./files/hello.txt"
	fileData, err := embedFS.ReadFile("files/hello.txt")
	if err != nil {
		panic(err)
	}
	fmt.Println("string(fileData): ", string(fileData))
}

Most examples I see specify how to use the go:embed directive to point to a particular file, and reading that particular file using ReadFile(). Or defining a directory in the go:embed directive, and using http.FS() to host that folder. However, I could not find an example of using // go:embed files/* to embed the entire directory, then trying to directly read a file located inside that directory using ReadFile(). Therefore, I wanted to know if this is even possible.

What did you expect to see?

Successful file read.

What did you see instead?

panic: open files/hello.txt: file does not exist
@seankhliao
Copy link
Member

Duplicate of #43698

@seankhliao seankhliao marked this as a duplicate of #43698 Mar 16, 2021
@bravecorvus
Copy link
Author

Doesn't seem like the same issue to me. #43698 seems to be referring to a linting problem. Mine is pointing to the behavior of ReadFile().

@seankhliao
Copy link
Member

Yes, and the proposed rule would catch the problem with your code: space in // go:embed

@bravecorvus
Copy link
Author

Gocha. Thanks

@golang golang locked and limited conversation to collaborators Mar 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants