<p>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.</p>
<p>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.</p>
<pre><code>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
}
}
}
}
</code></pre>
<p>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.</p>
<hr/>**评论:**<br/><br/>CodeAddict: <pre><p>Well peterSO solved this for me at <a href="http://stackoverflow.com/a/29337084/1681650" rel="nofollow">StackOverflow</a>.</p></pre>dchapes: <pre><p>With a name of "CodeAddict" you can't solve a trivial, basic, programming 101, if-you-can't-do-this-you-can't-program problem so you get someone else to do it for it??
IMO any such trivial "questions" should be deleted from SO else it becomes a giant rephrasing of the basic manuals/specs and explanations of how to write a fucking loop.</p></pre>printf_hello_world: <pre><p>For a beginner programmer, the connection between their problem and a "fucking loop" may not be immediately obvious. SO helps make that relationship clear.</p>
<p>Everyone starts from somewhere; perhaps we should encourage CodeAddict's enthusiasm rather than squash it from atop our pedestals. </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传