donglipi4495 2014-07-25 18:09
浏览 66
已采纳

使用通配符*引用目录中的文件

I'm trying to use this Go lang forum software https://github.com/kjk/fofou. It requires a config file in the top level forums directory to specify certain information about the forum (name, url etc). For example, the file that the software designer uses is forums/sumatrapdf_config.json

in main.go there is this function that reads forum config files

    func readForumConfigs(configDir string) error {
    pat := filepath.Join(configDir, "*_config.json")
    fmt.Println("path", pat)
    files, err := filepath.Glob(pat)
    fmt.Println("files", files, err)
    if err != nil {
        return err
    }
    if files == nil {
        return errors.New("No forums configured!")
    }
    for _, configFile := range files {
        var forum ForumConfig
        b, err := ioutil.ReadFile(configFile)
        if err != nil {
            return err
        }
        err = json.Unmarshal(b, &forum)
        if err != nil {
            return err
        }
        if !forum.Disabled {
            forums = append(forums, &forum)
        }
    }
    if len(forums) == 0 {
        return errors.New("All forums are disabled!")
    }
    return nil
}

I've played around with the second argument to the Join, calling it by the file name specifically and with the wildcard *, but I keep getting error messages telling me there are no files.

the log statements show the path that it's checking as well as the fact that no files are found
path forums/*funnyforum_config.json files [] 2014/07/25 10:34:11 Failed to read forum configs, err: No forums configured!

The same thing happens if I try to describe the config with a wildcard * as is done in the source code by the software creator

func readForumConfigs(configDir string) error { pat := filepath.Join(configDir, "*_config.json") fmt.Println("path", pat) files, err := filepath.Glob(pat) fmt.Println("files", files)

path forums/*_config.json files [] 2014/07/25 10:40:38 Failed to read forum configs, err: No forums configured!

In the forums directory, I have put various config files

funnyforum_config.json _config.json

as well as the config it came with

sumatrapdf_config.json
  • 写回答

1条回答 默认 最新

  • dongza6247 2014-07-25 18:22
    关注

    You're not checking the error from glob, you should, also you could implement it in a different way:

    func FilterDirs(dir, suffix string) ([]string, error) {
        files, err := ioutil.ReadDir(dir)
        if err != nil {
            return nil, err
        }
        res := []string{}
        for _, f := range files {
            if !f.IsDir() && strings.HasSuffix(f.Name(), suffix) {
                res = append(res, filepath.Join(dir, f.Name()))
            }
        }
        return res, nil
    }
    
    func FilterDirsGlob(dir, suffix string) ([]string, error) {
        return filepath.Glob(filepath.Join(dir, suffix))
    }
    
    func main() {
        fmt.Println(FilterDirs("/tmp", ".json"))
        fmt.Println(FilterDirsGlob("/tmp", "*.json"))
    }
    

    <kbd>playground</kbd>

    //edit

    From our discussion, you have to either use a full path /home/user/go/....../forums/ or a relative path ./forums/.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀