golang http.Dir()

0 0 golang gou8bedu8a00
奇衡三
奇衡三

声望值:76 0人

2018-08-21 13:08:20 提问

关注 0关注

收藏 0收藏, 204浏览

使用FileServer看到需要传一个FileSystem类型
http.FileServer(http.Dir("/public"))
FileSystem是个interface
clipboard.png

然后http.Dir实现了FileSystem

clipboard.png
不应该是http.Dir.Open("public")
为何可以直接http.Dir("public")?不解,有人给解答下?

请先 登录 后评论

3个回答

  • Cocos︷Creator201声望 2018-08-08 11:42

    Dir实际上是string type Dir string http.Dir("/public")可以认为是类型转换,不是函数
    请先 登录 后评论
  • JiYeon174声望 2018-08-08 11:44

    因为Dir实现了Open接口,http.Dir("/public")将/public字符串转换为了Dir.
    请先 登录 后评论
  • Python//孤狼134声望 2018-08-08 11:44

    // 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请求;
    请先 登录 后评论

注册新账号

悬赏追问
10
  • 10
  • 20
  • 50
  • 100
  • 200
  • 输入数值
发布追问