How to use context with filepath.Walk?

Is it possible to use context to manage data between WalkFunc invocations? For example, a recursive file linter wishing to report warnings for lots of nodes, but also remember the warning count at the end, could use something like context to maintain this state.

Could someone provide an example of a reasonably idiomatic way to store state during filepath.Walk?

Don’t use a context for that. Make the walk function a method of a suitable type and track the state on that type, or something similar. Contexts are for cancellation, and if you want to be able to cancel the walk you might want one. But not for tracking the totals.

3 Likes

The fileparh.Walk functor does not accept functions with altered signatures. Oh well, I’ll just weite my own Walk functor

There is no need to alter the signature. I meant like this:

https://play.golang.org/p/zM5xX-SWSw

2 Likes

Oh damn, thanks for example code! I’ll be sure to use a method rather than a function in the future to capture this behavior.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.