O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The net/http package provides the functions ServeFile and FileServer, which are designed to serve the static files. The ServeFile function just consumes the ResponseWriter and Request with the given file path argument and writes the content of the file to the response.

The FileServer function creates the whole Handler which consumes the FileSystem argument. The preceding example uses the Dir type, which implements the FileSystem interface. The FileSystem interface requires implementing the Open method, which consumes string and returns the actual File for the given path.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required