Compare two slices of strings

polaris · · 1032 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m able to get a list of files and folders from a directory, I&#39;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&#39;t to be avoided.</p> <pre><code>package main import ( &#34;fmt&#34; &#34;io/ioutil&#34; &#34;os&#34; &#34;strings&#34; ) func isDir(pth string) (bool) { fi, err := os.Stat(pth) if err != nil { return false } return fi.Mode().IsDir() } func main() { gcomputer := &#34;localhost&#34; location := fmt.Sprintf(&#34;\\\\%s\\c$\\Users\\&#34;, gcomputer) // Profiles to avoid avoid := []string{&#34;Administrator&#34;, &#34;Default&#34;, &#34;Public&#34;} // walk through files &amp; folders in the directory (location) &amp; return valid profiles files, _ := ioutil.ReadDir(location) for _, f := range files { fdir := []string{location, f.Name()} dpath := strings.Join(fdir, &#34;&#34;) if isDir(dpath) { for _, iavoid := range avoid { for iavoid != f.Name() { fmt.Println(dpath) break } break } } } } </code></pre> <p>I don&#39;t mind using a third-party module, I&#39;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 &#34;CodeAddict&#34; you can&#39;t solve a trivial, basic, programming 101, if-you-can&#39;t-do-this-you-can&#39;t-program problem so you get someone else to do it for it?? IMO any such trivial &#34;questions&#34; 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 &#34;fucking loop&#34; may not be immediately obvious. SO helps make that relationship clear.</p> <p>Everyone starts from somewhere; perhaps we should encourage CodeAddict&#39;s enthusiasm rather than squash it from atop our pedestals. </p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1032 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传