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

huangapple go评论82阅读模式
英文:

Using a wildcard * to reference files in a directory

问题

我正在尝试使用这个Go语言论坛软件https://github.com/kjk/fofou。它需要在顶级论坛目录中的配置文件来指定有关论坛的某些信息(名称、URL等)。例如,软件设计者使用的文件是forums/sumatrapdf_config.json

在main.go中,有这个函数来读取论坛配置文件:

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
}

我已经尝试过更改Join的第二个参数,分别通过文件名和通配符*来调用它,但是我一直收到告诉我没有文件的错误消息。

日志语句显示了它正在检查的路径以及没有找到文件的事实:

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

如果我尝试像软件创建者在源代码中所做的那样使用通配符*来描述配置,同样的情况也会发生:

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!

在论坛目录中,我放置了各种配置文件:

funnyforum_config.json _config.json

以及它附带的配置文件:

sumatrapdf_config.json
英文:

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

得分: 3

你没有检查glob的错误,你应该检查,另外你可以用不同的方式实现:

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

从我们的讨论中,你必须使用完整路径/home/user/go/....../forums/或者相对路径./forums/

英文:

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

huangapple
  • 本文由 发表于 2014年7月26日 02:09:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/24961974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定