GoLang中使用切片进行字符串比较

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

GoLang string comparison with Slices

问题

我能够从目录中获取文件和文件夹的列表,我编写了一个名为isDir的函数,如果路径是一个目录,则返回True。

现在我的问题是,我想确保列出的文件夹中没有与切片中的字符串列表匹配的文件夹。我目前的代码可能会跳过第一个匹配,但无论如何都会打印出其他所有内容。我需要处理那些不应该被避免的文件夹。

代码适用于Windows 7/8目录,但如果提供了Linux示例,我应该也能使其工作。

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "strings"
  7. )
  8. func isDir(pth string) bool {
  9. fi, err := os.Stat(pth)
  10. if err != nil {
  11. return false
  12. }
  13. return fi.Mode().IsDir()
  14. }
  15. func main() {
  16. gcomputer := "localhost"
  17. location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
  18. // 要避免的配置文件
  19. avoid := []string{"Administrator", "Default", "Public"}
  20. // 遍历目录(location)中的文件和文件夹,并返回有效的配置文件
  21. files, _ := ioutil.ReadDir(location)
  22. for _, f := range files {
  23. fdir := []string{location, f.Name()}
  24. dpath := strings.Join(fdir, "")
  25. if isDir(dpath) {
  26. shouldAvoid := false
  27. for _, iavoid := range avoid {
  28. if iavoid == f.Name() {
  29. shouldAvoid = true
  30. break
  31. }
  32. }
  33. if !shouldAvoid {
  34. fmt.Println(dpath)
  35. }
  36. }
  37. }
  38. }

我不介意使用第三方模块,我已经在这个问题上工作了很长时间,开始变得有点冷静,使得理解文档有点困难。任何提示都将不胜感激。谢谢。

英文:

I'm able to get a list of files and folders from a directory, I've written a function called isDir to return True if the path is a directory.

Now my problem is that I want to make sure that none of the folders listed match a list of strings in a slice. The code I have might skip the first match but it will print out everything else anyways. I need to process the folders that aren't to be avoided.

Code is for Windows 7/8 directories but I should be able to get a Linux sample working too should one be provided.

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "strings"
  7. )
  8. func isDir(pth string) (bool) {
  9. fi, err := os.Stat(pth)
  10. if err != nil {
  11. return false
  12. }
  13. return fi.Mode().IsDir()
  14. }
  15. func main() {
  16. gcomputer := "localhost"
  17. location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
  18. // Profiles to avoid
  19. avoid := []string{"Administrator", "Default", "Public"}
  20. // walk through files & folders in the directory (location) & return valid profiles
  21. files, _ := ioutil.ReadDir(location)
  22. for _, f := range files {
  23. fdir := []string{location, f.Name()}
  24. dpath := strings.Join(fdir, "")
  25. if isDir(dpath) {
  26. for _, iavoid := range avoid {
  27. for iavoid != f.Name() {
  28. fmt.Println(dpath)
  29. break
  30. }
  31. break
  32. }
  33. }
  34. }
  35. }

I don't mind using a third-party module, I've been working on this too long and starting to lose my cool, making understanding the docs a bit difficult. Any tips would be appreciated. Thank you.

答案1

得分: 1

如果要避免的字符串列表变得更长,您可能不希望为每个目录迭代它们。我可以建议对@peterSO的答案进行小修改:

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. )
  6. var avoidanceSet = map[string]bool{
  7. "Administrator": true,
  8. "Default": true,
  9. "Public": true,
  10. }
  11. func avoid(name string) bool {
  12. _, inSet := avoidanceSet[name]
  13. return inSet
  14. }
  15. func main() {
  16. gcomputer := "localhost"
  17. location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
  18. files, err := ioutil.ReadDir(location)
  19. if err != nil {
  20. fmt.Println(err)
  21. return
  22. }
  23. for _, f := range files {
  24. if f.IsDir() && !avoid(f.Name()) {
  25. dpath := location + f.Name()
  26. fmt.Println(dpath)
  27. }
  28. }
  29. }

这是一个使用Go语言编写的代码示例,它使用一个名为avoidanceSet的映射来存储要避免的字符串。在avoid函数中,它会检查给定的名称是否在avoidanceSet中,如果是,则返回true,否则返回false。在main函数中,它遍历目录中的文件,并根据avoid函数的返回值来确定是否打印目录路径。

英文:

If your list of strings to avoid gets bigger, you might not want to be iterating over them for every directory. Might I suggest a small modification to @peterSO's answer:

<!-- language: golang -->
package main

  1. import (
  2. &quot;fmt&quot;
  3. &quot;io/ioutil&quot;
  4. )
  5. var avoidanceSet = map[string]bool{
  6. &quot;Administrator&quot;: true,
  7. &quot;Default&quot;: true,
  8. &quot;Public&quot;: true,
  9. }
  10. func avoid(name string) bool {
  11. _, inSet := avoidanceSet[name]
  12. return inSet
  13. }
  14. func main() {
  15. gcomputer := &quot;localhost&quot;
  16. location := fmt.Sprintf(&quot;\\\\%s\\c$\\Users\\&quot;, gcomputer)
  17. files, err := ioutil.ReadDir(location)
  18. if err != nil {
  19. fmt.Println(err)
  20. return
  21. }
  22. for _, f := range files {
  23. if f.IsDir() &amp;&amp; !avoid(f.Name()) {
  24. dpath := location + f.Name()
  25. fmt.Println(dpath)
  26. }
  27. }
  28. }

答案2

得分: 0

例如,

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. )
  6. func avoid(name string) bool {
  7. profiles := []string{"Administrator", "Default", "Public"}
  8. for _, p := range profiles {
  9. if name == p {
  10. return true
  11. }
  12. }
  13. return false
  14. }
  15. func main() {
  16. gcomputer := "localhost"
  17. location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
  18. files, err := ioutil.ReadDir(location)
  19. if err != nil {
  20. fmt.Println(err)
  21. return
  22. }
  23. for _, f := range files {
  24. if f.IsDir() && !avoid(f.Name()) {
  25. dpath := location + f.Name()
  26. fmt.Println(dpath)
  27. }
  28. }
  29. }
英文:

For example,

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;io/ioutil&quot;
  5. )
  6. func avoid(name string) bool {
  7. profiles := []string{&quot;Administrator&quot;, &quot;Default&quot;, &quot;Public&quot;}
  8. for _, p := range profiles {
  9. if name == p {
  10. return true
  11. }
  12. }
  13. return false
  14. }
  15. func main() {
  16. gcomputer := &quot;localhost&quot;
  17. location := fmt.Sprintf(&quot;\\\\%s\\c$\\Users\\&quot;, gcomputer)
  18. files, err := ioutil.ReadDir(location)
  19. if err != nil {
  20. fmt.Println(err)
  21. return
  22. }
  23. for _, f := range files {
  24. if f.IsDir() &amp;&amp; !avoid(f.Name()) {
  25. dpath := location + f.Name()
  26. fmt.Println(dpath)
  27. }
  28. }
  29. }

huangapple
  • 本文由 发表于 2015年3月30日 08:46:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/29336792.html
匿名

发表评论

匿名网友

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

确定