Go

Go

Made by DeepSource

Use http.FileSystem(http.Dir(...)) instead of gin.Dir(...,true) GO-R1000

Anti-pattern
Minor
Autofix

If listDirectory is true for gin.Dir, it could be directly replaced by http.FileSystem(http.Dir(...)) to be more explicit of the behavior of the function.

Source

// Dir returns a http.Filesystem that can be used by http.FileServer(). It is used internally
// in router.Static().
// if listDirectory == true, then it works the same as http.Dir() otherwise it returns
// a filesystem that prevents http.FileServer() to list the directory files.
func Dir(root string, listDirectory bool) http.FileSystem {
    fs := http.Dir(root)
    if listDirectory {
        return fs
    }
    return &onlyFilesFS{fs}
}

Documentation of gin.Dir also notes the same if true is passed as an argument to the listDirectory parameter.

Bad practice

customFS := uFS{
    FileSystem: gin.Dir(global.StaticPath, true),
}
r.Use(static.Serve("/", customFS))

Recommended

customFS := uFS{
    FileSystem: http.FileSystem(http.Dir(global.StaticPath)),
}
r.Use(static.Serve("/", customFS))