在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/GO/ golang http.Dir()

golang http.Dir()

使用FileServer看到需要傳一個(gè)FileSystem類型
http.FileServer(http.Dir("/public"))
FileSystem是個(gè)interface
clipboard.png

然后http.Dir實(shí)現(xiàn)了FileSystem

clipboard.png
不應(yīng)該是http.Dir.Open("public")
為何可以直接http.Dir("public")?不解,有人給解答下?

回答
編輯回答
落殤

Dir實(shí)際上是string

type Dir string

http.Dir("/public")可以認(rèn)為是類型轉(zhuǎn)換,不是函數(shù)

2018年7月27日 23:08
編輯回答
舊言

因?yàn)镈ir實(shí)現(xiàn)了Open接口,http.Dir("/public")將/public字符串轉(zhuǎn)換為了Dir.

2017年6月19日 13:13
編輯回答
大濕胸
// 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)
}

這樣看應(yīng)該比較清楚吧

http.Dir("/public")是利用本地tmp目錄實(shí)現(xiàn)一個(gè)文件系統(tǒng);
http.FileServer(http.Dir("/public"))返回一個(gè)Handler,其用來處理訪問本地"/tmp"文件夾的HTTP請求;

2017年5月23日 00:17