MAO-EYE 2013-02-03 02:17 采纳率: 100%
浏览 229
已采纳

在 Go 中列出目录

I've been trying to figure out how to simply list the files and folders in a single directory in Go.

I've found filepath.Walk, but it goes into sub-directories automatically, which I don't want. All of my other searches haven't turned anything better up.

I'm sure that this functionality exists, but it's been really hard to find. Let me know if anyone knows where I should look. Thanks.

转载于:https://stackoverflow.com/questions/14668850/list-directory-in-go

  • 写回答

4条回答 默认 最新

  • 喵-见缝插针 2013-02-03 02:29
    关注

    You can try using the ReadDir function in the io/ioutil package. Per the docs:

    ReadDir reads the directory named by dirname and returns a list of sorted directory entries.

    The resulting slice contains os.FileInfo types, which provide the methods listed here. Here is a basic example that lists the name of everything in the current directory (folders are included but not specially marked - you can check if an item is a folder by using the IsDir() method):

    package main
    
    import (
        "fmt"
        "io/ioutil"
         "log"
    )
    
    func main() {
        files, err := ioutil.ReadDir("./")
        if err != nil {
            log.Fatal(err)
        }
    
        for _, f := range files {
                fmt.Println(f.Name())
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛