登录
首页 >  Golang >  Go问答

深入解析“Readdir”和“Open”的包装方法

来源:stackoverflow

时间:2024-02-08 14:18:21 427浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《深入解析“Readdir”和“Open”的包装方法》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我是 golang 新手。有人可以解释一下“readdir”和“open”包装方法是如何工作的吗?

此示例来自 golang 文档。 https://pkg.go.dev/net/http#example-fileserver-dotfilehiding

更具体地说,在“readdir”方法中,语句 files, err := f.file.readdir(n) 有 n 个 int 值,但是它从哪里传递以及它如何工作。程序中的任何地方都没有调用“readdir”方法。

类似地,在“打开”包装器 file 中,err := fsys.filesystem.open(name)

package main

import (
    "io/fs"
    "log"
    "net/http"
    "strings"
)

// containsDotFile reports whether name contains a path element starting with a period.
// The name is assumed to be a delimited by forward slashes, as guaranteed
// by the http.FileSystem interface.
func containsDotFile(name string) bool {
    parts := strings.Split(name, "/")
    for _, part := range parts {
        if strings.HasPrefix(part, ".") {
            return true
        }
    }
    return false
}

// dotFileHidingFile is the http.File use in dotFileHidingFileSystem.
// It is used to wrap the Readdir method of http.File so that we can
// remove files and directories that start with a period from its output.
type dotFileHidingFile struct {
    http.File
}
f
// Readdir is a wrapper around the Readdir method of the embedded File
// that filters out all files that start with a period in their name.
func (f dotFileHidingFile) Readdir(n int) (fis []fs.FileInfo, err error) {
    files, err := f.File.Readdir(n)
    for _, file := range files { // Filters out the dot files
        if !strings.HasPrefix(file.Name(), ".") {
            fis = append(fis, file)
        }
    }
    return
}

// dotFileHidingFileSystem is an http.FileSystem that hides
// hidden "dot files" from being served.
type dotFileHidingFileSystem struct {
    http.FileSystem
}

// Open is a wrapper around the Open method of the embedded FileSystem
// that serves a 403 permission error when name has a file or directory
// with whose name starts with a period in its path.
func (fsys dotFileHidingFileSystem) Open(name string) (http.File, error) {
    if containsDotFile(name) { // If dot file, return 403 response
        return nil, fs.ErrPermission
    }

    file, err := fsys.FileSystem.Open(name)
    if err != nil {
        return nil, err
    }
    return dotFileHidingFile{file}, err
}

func main() {
    fsys := dotFileHidingFileSystem{http.Dir(".")}
    http.Handle("/", http.FileServer(fsys))
    log.Fatal(http.ListenAndServe(":8080", nil))
}

正确答案


dotFileHidingFileSystem 类型包装了 http.FileSystem,它本身就是一个 http.FileSystem。

dotFileHidingFile 类型包装了 http.File,它本身就是一个 http.File。

因为这两个结构类型 embed 是包装值,所以包装值上的所有方法都将提升为包装类型上的方法,除非包装类型本身实现该方法。如果您不熟悉嵌入概念,请阅读 Effective Go section on embedding

net/http FileServer 调用 http.FileSystem 和 http.File 接口上的方法。

file server调用文件系统Open方法打开文件。该方法的 dotFileHidingFileSystem 实现调用包装的文件系统来打开文件并返回该文件的 dotFileHidingFile 包装器。

如果文件是目录,则 file server 调用 file Readdir 方法来获取目录中文件的列表。文件服务器指定n 的值。 Readdir 方法的 dotFileHidingFile 实现通过包装文件 Readdir 方法调用并从结果中过滤出点文件。

有关 Readdir 的 n 参数的文档,请参阅 ReadDirFile 文档。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《深入解析“Readdir”和“Open”的包装方法》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>