Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 go 中获取文件系统的最简单方法是下面的代码。
http.Handle("/files", http.StripPrefix(pathPrefix, http.FileServer(root)))
但是出于客观设计的目的,我更喜欢将函数体包裹在这样的方法中。
f := file{} http.Handle("/download", f.download) http.Handle("/upload", f.upload)
我应该如何将代码内容包装在文件结构方法中?
您可以通过使用http.HandleFunc而不是http.Handle:
http.HandleFunc
http.Handle
f := file{} http.HandleFunc("/download", f.download) http.HandleFunc("/upload", f.upload)
假设它有正确的签名(即假设它f.download被定义为类似的东西func (f file) download(w http.ResponseWriter, r *http.Request)),那么它应该做你想做的事。
f.download
func (f file) download(w http.ResponseWriter, r *http.Request)