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

embed: http中使用embed,取消相对路径后,路径中/需要修改为\才能读取到静态资源 #50408

Closed
cnwangfei opened this issue Jan 2, 2022 · 2 comments

Comments

@cnwangfei
Copy link

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

$ go 1.16.11

我是这样做的改动

HttpEmbed

package httpEmbed

import (
	"embed"
	"io/fs"
	"path"
	"path/filepath"
)

type Fs struct {
	*embed.FS        // 静态资源
	Path      string // 设置embed文件到静态资源的相对路径,也就是embed注释里的路径
}

func (f Fs) Open(name string) (fs.File, error) {
	// 拼接相对路径
	fullName := filepath.Join(f.Path, filepath.FromSlash(path.Clean("/"+name)))
	// 修改http路径中的\为/
	fullName = filepath.ToSlash(fullName)
	//fmt.Println(fullName)
	fs, err := f.FS.Open(fullName)
	return fs, err
}
@mdlayher
Copy link
Member

mdlayher commented Jan 2, 2022

Sorry, we can't help with non-English issues on the tracker. See: https://github.com/golang/go/wiki/NonEnglish and for questions: https://github.com/golang/go/wiki/Questions.

@mdlayher mdlayher closed this as completed Jan 2, 2022
@ianlancetaylor
Copy link
Contributor

The io.FS package always uses forward slashes. See https://pkg.go.dev/io/fs#ValidPath.

@golang golang locked and limited conversation to collaborators Jan 2, 2023
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

4 participants