使用文件服务器为我的单个html页面提供服务

I'm trying to build a sample web application demonstrating techniques using at the back-end, serving based requests and , in the front-end (I'm not using html/template package).

FileServer "returns a handler that serves HTTP requests with the contents of the file system rooted at root."

supose that I'm publishing my static folder that contains index.html and scripts folder holding some javascript files.

How can I prevent the client from viewing my js files (publishing just the index.html at /) ?

You can easily restrict the FileServer, which is a HttpHandler by wrapping another HttpHandler around that. For example, take this wrapper which ONLY allows *.js files to be served:

func GlobFilterHandler(h http.Handler, pattern string) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        path := r.URL.Path

        fileName := filepath.Base(path)

        if ok, err := filepath.Match(pattern, fileName); !ok || err != nil {
            if err != nil {
                log.Println("Error in pattern match:", err)
            }

            http.NotFound(w, r)
            return
        }

        h.ServeHTTP(w, r)
    })
}

func main() {
    fileHandler := http.FileServer(http.Dir("/tmp/dtest"))
    wrappedHandler := GlobFilterHandler(fileHandler, "*.js")
}

You can find a blog post here which describes the basic idea pretty good.

Another option you have is to extend on http.Dir and make your own http.FileSystem implementation which does exactly what you want:

type GlobDir struct {
    Dir     http.Dir
    Pattern string
}

func (d GlobDir) Open(name string) (http.File, error) {
    baseName := filepath.Base(name)

    if ok, err := filepath.Match(d.Pattern, baseName); !ok || err != nil {
        if err != nil {
            return nil, err
        }
        return nil, fmt.Errorf("%s not match GlobDir pattern.", baseName)
    }

    return d.Dir.Open(name)
}

func main() {
    fileHandler := http.FileServer(GlobDir{
        Dir: http.Dir("/tmp/dtest"),
        Pattern: "*.js",
    })

    http.ListenAndServe(":8080", fileHandler)
}

The second solution implements the http.FileSystem interface which is accepted by http.FileServer. It checks whether the input file name matches the supplied pattern and then hands control down to the original http.Dir. This is probably the way you want to go here.