简体   繁体   中英

Implicit interface call function

Looking for some examples to server static files with net/http package in Golang, I found the type Dir which implements FileSystem interface.

Some examples show You can server static files with the following:

http.Handle("/", http.FileServer(http.Dir("/tmp")))

What exactly is http.Dir("/tmp") ? It looks like a constructor function for FileSystem .

http.Dir("/tmp") is actually a type conversion where you convert the string /tmp into the http.Dir type. Looking at the docs , you will see that http.Dir is actually a string type. Hence, this type conversion works.

In addition, the http.Dir type also implements the func Open(name string) (File, error) function. Hence, it can be used anywhere where a FileSystem interface is used.

You can also check out the func ServeFile(w ResponseWriter, r *Request, name string) function in the net/http package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM