rvillablanca
rvillablanca

Reputation: 1646

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.

Upvotes: 0

Views: 52

Answers (1)

ivan.sim
ivan.sim

Reputation: 9268

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.

Upvotes: 1

Related Questions