Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Golang组合模式实现文件系统结构管理_创想鸟

Golang组合模式实现文件系统结构管理

组合模式通过统一接口处理文件和目录,实现递归遍历与大小统计,Go中用FileSystemNode接口统一文件和目录操作,Directory包含多个节点并递归调用,简化树形结构管理。

golang组合模式实现文件系统结构管理

在 Go 语言中,组合模式非常适合用来表示树形结构,比如文件系统中的目录和文件关系。通过统一接口处理文件和目录,可以简化操作逻辑,实现递归遍历、统计大小、打印结构等功能。

定义组件接口

我们需要一个统一的接口来表示文件系统中的节点,无论是文件还是目录,都实现相同的方法。

Component 接口定义了所有节点共有的行为,比如获取名称、获取大小、打印路径等。

代码如下:

package maintype FileSystemNode interface {    GetName() string    GetSize() int    Print(indent string)}

实现文件节点

文件是最小单位,它不包含其他节点,只保存名称和大小。

立即学习“go语言免费学习笔记(深入)”;

File 结构体实现 FileSystemNode 接口:

type File struct {    name string    size int}func (f *File) GetName() string {    return f.name}func (f *File) GetSize() int {    return f.size}func (f *File) Print(indent string) {    println(indent + f.name + " (file, size: " + string(rune(f.size)) + ")")}

注意:上面 Print 中的 size 转字符串方式有误,应使用 strconv.Itoa。

修正后的 Print 方法:

import "fmt"func (f *File) Print(indent string) {    fmt.Println(indent + f.name + " (file, size: " + fmt.Sprintf("%d", f.size) + ")")}

实现目录节点(组合容器)

Directory 可以包含多个 FileSystemNode,包括文件和其他目录,体现“组合”特性。

type Directory struct {    name   string    nodes  []FileSystemNode}func (d *Directory) GetName() string {    return d.name}func (d *Directory) GetSize() int {    var total int    for _, node := range d.nodes {        total += node.GetSize()    }    return total}func (d *Directory) Add(node FileSystemNode) {    d.nodes = append(d.nodes, node)}func (d *Directory) Print(indent string) {    fmt.Println(indent + d.name + " (dir, size: " + fmt.Sprintf("%d", d.GetSize()) + ")")    for _, node := range d.nodes {        node.Print(indent + "  ")    }}

使用示例:构建文件系统结构

现在我们可以创建一个模拟的文件系统,包含目录和子目录。

func main() {    root := &Directory{name: "root"}    docs := &Directory{name: "docs"}    img := &Directory{name: "images"}    file1 := &File{name: "readme.txt", size: 1024}    file2 := &File{name: "photo.jpg", size: 2048}    file3 := &File{name: "avatar.png", size: 512}    docs.Add(file1)    img.Add(file2)    img.Add(file3)    root.Add(docs)    root.Add(img)    // 打印整个结构    root.Print("")    // 输出总大小    fmt.Printf("Total size: %d bytesn", root.GetSize())}

输出结果类似:

root (dir, size: 3584)  docs (dir, size: 1024)    readme.txt (file, size: 1024)  images (dir, size: 2560)    photo.jpg (file, size: 2048)    avatar.png (file, size: 512)Total size: 3584 bytes

组合模式让客户端代码无需区分文件和目录,统一调用接口即可完成操作,结构清晰,扩展性强。

基本上就这些,核心是接口统一和递归处理。添加删除节点也很方便,适合构建树形管理结构。

以上就是Golang组合模式实现文件系统结构管理的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1404361.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
如何检查并修复已损坏的Golang安装
上一篇 2025年12月15日 20:25:18
如何配置GOPRIVATE环境变量来拉取Golang私有仓库模块
下一篇 2025年12月15日 20:25:31

相关推荐

发表回复

登录后才能评论
关注微信