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

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

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"))
    }
    

    playground

    //edit

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题