0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.