使用FileServer看到需要传一个FileSystem类型
http.FileServer(http.Dir("/public"))
FileSystem是个interface
然后http.Dir实现了FileSystem
不应该是http.Dir.Open("public")
为何可以直接http.Dir("public")?不解,有人给解答下?
使用FileServer看到需要传一个FileSystem类型
http.FileServer(http.Dir("/public"))
FileSystem是个interface
然后http.Dir实现了FileSystem
不应该是http.Dir.Open("public")
为何可以直接http.Dir("public")?不解,有人给解答下?
// A Dir implements FileSystem using the native file system restricted to a
// specific directory tree.
//
// While the FileSystem.Open method takes '/'-separated paths, a Dir's string
// value is a filename on the native file system, not a URL, so it is separated
// by filepath.Separator, which isn't necessarily '/'.
//
// Note that Dir will allow access to files and directories starting with a
// period, which could expose sensitive directories like a .git directory or
// sensitive files like .htpasswd. To exclude files with a leading period,
// remove the files/directories from the server or create a custom FileSystem
// implementation.
//
// An empty Dir is treated as ".".
type Dir string
func (d Dir) Open(name string) (File, error) {
....
}
// A FileSystem implements access to a collection of named files.
// The elements in a file path are separated by slash ('/', U+002F)
// characters, regardless of host operating system convention.
type FileSystem interface {
Open(name string) (File, error)
}
这样看应该比较清楚吧
http.Dir("/public")是利用本地tmp目录实现一个文件系统;
http.FileServer(http.Dir("/public"))返回一个Handler,其用来处理访问本地"/tmp"文件夹的HTTP请求;
7 回答5.3k 阅读
6 回答6.9k 阅读✓ 已解决
4 回答2.3k 阅读
1 回答2k 阅读✓ 已解决
1 回答3.4k 阅读
2 回答2.2k 阅读
1 回答2.1k 阅读
Dir
实际上是string
http.Dir("/public")
可以认为是类型转换,不是函数