0
尋找一些在Golang中使用net/http
包尋找服務器靜態文件的例子,我發現Dir
這個類型實現了FileSystem
接口。隱式接口調用函數
一些例子顯示具有以下你可以服務器的靜態文件:
http.Handle("/", http.FileServer(http.Dir("/tmp")))
到底是什麼http.Dir("/tmp")
?它看起來像是FileSystem
的構造函數。
尋找一些在Golang中使用net/http
包尋找服務器靜態文件的例子,我發現Dir
這個類型實現了FileSystem
接口。隱式接口調用函數
一些例子顯示具有以下你可以服務器的靜態文件:
http.Handle("/", http.FileServer(http.Dir("/tmp")))
到底是什麼http.Dir("/tmp")
?它看起來像是FileSystem
的構造函數。
http.Dir("/tmp")
實際上是一種類型轉換,您可以將字符串/tmp
轉換爲http.Dir
類型。看看docs,你會發現http.Dir
實際上是一個字符串類型。因此,這種類型轉換工作。
另外,http.Dir
類型也實現了func Open(name string) (File, error)
函數。因此,它可以用於任何使用了接口的地方。
您還可以查看net/http
包中的func ServeFile(w ResponseWriter, r *Request, name string)
功能。