Go 如何';禁用目录列表';和';自定义404页';同时处理

Go 如何';禁用目录列表';和';自定义404页';同时处理,go,gorilla,Go,Gorilla,我在http服务器中使用gorilla mux进行路由 https://github.com/gorilla/mux 这是我禁用目录列表的代码 type justFilesFilesystem struct { fs http.FileSystem // readDirBatchSize - configuration parameter for Readdir func readDirBatchSize int } func (fs justFile

我在http服务器中使用gorilla mux进行路由

https://github.com/gorilla/mux
这是我禁用目录列表的代码

type justFilesFilesystem struct {
  fs               http.FileSystem
  // readDirBatchSize - configuration parameter for Readdir func
  readDirBatchSize int
}

func (fs justFilesFilesystem) Open(name string) (http.File, error) {
  f, err := fs.fs.Open(name)
  if err != nil {
    return nil, err
  }
  return neuteredStatFile{File: f, readDirBatchSize: fs.readDirBatchSize}, nil
}

type neuteredStatFile struct {
  http.File
  readDirBatchSize int
}

func (e neuteredStatFile) Stat() (os.FileInfo, error) {
  s, err := e.File.Stat()
  if err != nil {
    return nil, err
  }
  if s.IsDir() {
  LOOP:
    for {
      fl, err := e.File.Readdir(e.readDirBatchSize)
      switch err {
      case io.EOF:
        break LOOP
      case nil:
        for _, f := range fl {
          if f.Name() == "" {
            return s, err
          }
        }
      default:
        return nil, err
      }
    }
    return nil, os.ErrNotExist
  }
  return s, err
}
这是我的主要职责

  mux := mux.NewRouter()
  // This line why not work?
  mux.NotFoundHandler = http.HandlerFunc(NotFound)

  mux.HandleFunc("/index",HandleIndex)

  fs := justFilesFilesystem{fs: http.Dir("assets"), readDirBatchSize: 0}
  staticFileHandler := http.StripPrefix("", http.FileServer(fs))
  mux.PathPrefix("").Handler(staticFileHandler).Methods("GET")
最后是我的404句柄函数

func NotFound(w http.ResponseWriter, r *http.Request) {

  fmt.Fprint(w, "404")
}

问题是如果找不到文件,如何处理自定义404页面错误,并同时禁用目录列表。

虽然您的问题只有一半答案,但请检查相关问题:不工作我已检查