英文:
GoLang string comparison with Slices
问题
我能够从目录中获取文件和文件夹的列表,我编写了一个名为isDir的函数,如果路径是一个目录,则返回True。
现在我的问题是,我想确保列出的文件夹中没有与切片中的字符串列表匹配的文件夹。我目前的代码可能会跳过第一个匹配,但无论如何都会打印出其他所有内容。我需要处理那些不应该被避免的文件夹。
代码适用于Windows 7/8目录,但如果提供了Linux示例,我应该也能使其工作。
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func isDir(pth string) bool {
fi, err := os.Stat(pth)
if err != nil {
return false
}
return fi.Mode().IsDir()
}
func main() {
gcomputer := "localhost"
location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
// 要避免的配置文件
avoid := []string{"Administrator", "Default", "Public"}
// 遍历目录(location)中的文件和文件夹,并返回有效的配置文件
files, _ := ioutil.ReadDir(location)
for _, f := range files {
fdir := []string{location, f.Name()}
dpath := strings.Join(fdir, "")
if isDir(dpath) {
shouldAvoid := false
for _, iavoid := range avoid {
if iavoid == f.Name() {
shouldAvoid = true
break
}
}
if !shouldAvoid {
fmt.Println(dpath)
}
}
}
}
我不介意使用第三方模块,我已经在这个问题上工作了很长时间,开始变得有点冷静,使得理解文档有点困难。任何提示都将不胜感激。谢谢。
英文:
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.
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func isDir(pth string) (bool) {
fi, err := os.Stat(pth)
if err != nil {
return false
}
return fi.Mode().IsDir()
}
func main() {
gcomputer := "localhost"
location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
// Profiles to avoid
avoid := []string{"Administrator", "Default", "Public"}
// walk through files & folders in the directory (location) & return valid profiles
files, _ := ioutil.ReadDir(location)
for _, f := range files {
fdir := []string{location, f.Name()}
dpath := strings.Join(fdir, "")
if isDir(dpath) {
for _, iavoid := range avoid {
for iavoid != f.Name() {
fmt.Println(dpath)
break
}
break
}
}
}
}
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的答案进行小修改:
package main
import (
"fmt"
"io/ioutil"
)
var avoidanceSet = map[string]bool{
"Administrator": true,
"Default": true,
"Public": true,
}
func avoid(name string) bool {
_, inSet := avoidanceSet[name]
return inSet
}
func main() {
gcomputer := "localhost"
location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
files, err := ioutil.ReadDir(location)
if err != nil {
fmt.Println(err)
return
}
for _, f := range files {
if f.IsDir() && !avoid(f.Name()) {
dpath := location + f.Name()
fmt.Println(dpath)
}
}
}
这是一个使用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
import (
"fmt"
"io/ioutil"
)
var avoidanceSet = map[string]bool{
"Administrator": true,
"Default": true,
"Public": true,
}
func avoid(name string) bool {
_, inSet := avoidanceSet[name]
return inSet
}
func main() {
gcomputer := "localhost"
location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
files, err := ioutil.ReadDir(location)
if err != nil {
fmt.Println(err)
return
}
for _, f := range files {
if f.IsDir() && !avoid(f.Name()) {
dpath := location + f.Name()
fmt.Println(dpath)
}
}
}
答案2
得分: 0
例如,
package main
import (
"fmt"
"io/ioutil"
)
func avoid(name string) bool {
profiles := []string{"Administrator", "Default", "Public"}
for _, p := range profiles {
if name == p {
return true
}
}
return false
}
func main() {
gcomputer := "localhost"
location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
files, err := ioutil.ReadDir(location)
if err != nil {
fmt.Println(err)
return
}
for _, f := range files {
if f.IsDir() && !avoid(f.Name()) {
dpath := location + f.Name()
fmt.Println(dpath)
}
}
}
英文:
For example,
package main
import (
"fmt"
"io/ioutil"
)
func avoid(name string) bool {
profiles := []string{"Administrator", "Default", "Public"}
for _, p := range profiles {
if name == p {
return true
}
}
return false
}
func main() {
gcomputer := "localhost"
location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
files, err := ioutil.ReadDir(location)
if err != nil {
fmt.Println(err)
return
}
for _, f := range files {
if f.IsDir() && !avoid(f.Name()) {
dpath := location + f.Name()
fmt.Println(dpath)
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论