<p>Let's say I want all the directories - only the directories - of a path recursively and I'm using filepath.Walk.</p>
<p>filepath.Walk takes a path and a function of type func(string, os.fileinfo, error) error </p>
<p>Is there a way to pass the functionality I want in the function directly to filepath.Walk. Must I create a wrapper function to get at the data. My problem is that what if based on a different argument I wanted just the files and not directories, I would then have another wrapper function. How about the files and directories that star with certain characters. Yet another wrapper function. </p>
<p>It seems there should be a way to incorporate that functionality into the function I'm already passing to filepath.Walk. I hope this is making sense.</p>
<hr/>**评论:**<br/><br/>peterbourgon: <pre><p>The function you pass to walk is going to see every file and directory in the tree. It's up to you to put logic in that function to skip over the things you don't care about. So you don't need a wrapper function, necessarily, but you do need some kind of logic.</p></pre>ops-man: <pre><p>How do I get the data without the wrapper. Currently I'm using a wrapper with a closure passed to filepath.Walk so I have access to my custom type within WalkFN... </p></pre>raff99: <pre><p>You should be able to define a structure with the various options you want to pass and then define a "walker" method on that structure, that you can pass to filepath.Walk.</p>
<p>Something like (completely untested):</p>
<pre><code>type WalkerConfig struct {
parseFiles bool
parseDirs bool
parsePrefix string
}
func (c WalkerConfig) walker(path string, info os.FileInfo, err error) error {
if c.parseFiles && ! info.isDir() {
// do something with the file
}
if c.parseDirs and info.IsDir() {
// do something with the folder
}
if c.ParsePrefix != "" && strings.HasPrefix(info.Name(), c.ParsePrefix) {
// do something with the match
}
return nil
}
</code></pre>
<p>then when you call filepath.Walk you pass a "configured" walker:</p>
<pre><code>c := WalkerConfig{parseFiles: true, parsePrefix: "x"}
filepath.Walk("/", c.walker)
</code></pre></pre>ops-man: <pre><p>Thank you. That's what I needed.</p></pre>nevyn: <pre><p>I think what you are asking is if you need to mix your filtering logic with your "I've got a file node" logic or if you can keep them apart ... there are a few ways to manage this, from having either the filtering logic or the file node logic in a different function and calling them; or having a configurable filter function as raff9 said; to just using a comment to separate the two pieces.</p>
<p>Might want to read:</p>
<p><a href="https://xojoc.pw/justcode/golang-file-tree-traversal.html" rel="nofollow">https://xojoc.pw/justcode/golang-file-tree-traversal.html</a></p></pre>ops-man: <pre><p>I read the article before I wrote any code. I have working code implementing the -dironly, -fileonly and - maxdepth=X flags. At one point my logic, my walk, and my parse are all separate. There was a lot of repeated code and it was evident - even to a noob - as I added functionality this was going to turn into a mess. So, I refactored which cleaned it up a bit. I came across this video <a href="https://youtu.be/5buaPyJ0XeQ" rel="nofollow">https://youtu.be/5buaPyJ0XeQ</a> by Dave Cheney and was wondering if I could pass functionality the same way to the walk function - more in a functional way. I need a way to scale the functionality of my application without turning my code into if/else hell.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传