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/.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度