英文:
How to pass a Map to a callback function in Go
问题
我正在尝试编写一个Go程序,用于遍历目录并查找特定文件,并将该信息存储在一个Map中。以下是我目前的代码。
type MyFile struct {
Name string
FilePath string
FileMD5Hash [16]byte
}
func visit(path string, f os.FileInfo, err error) error {
fileName := f.Name()
if !f.IsDir() && strings.Contains(strings.ToLower(fileName), "myfile") {
df := parseFile(path)
fmt.Printf("Visited: %s [%x], %s, %s\n", df.FilePath)
}
return nil
}
func parseFile(path string)...
func check(e error) ...
func WalkDir(path string) {
err := filepath.Walk(path, visit)
fmt.Printf("filepath.Walk() returned %v\n", err)
}
请注意,我不确定如何将Map传递给visit函数,因为它是一个回调函数。
英文:
I am trying to write a Go program to walk down a directory and find certain file and store that information in a Map. Here is what I have so far.
I am not sure how to pass the Map to the visit function since it's a callback function.
..
type MyFile struct {
Name string
FilePath string
FileMD5Hash [16]byte
}
func visit(path string, f os.FileInfo, err error) error {
fileName := f.Name()
if !f.IsDir() && strings.Contains(strings.ToLower(fileName), "myfile") {
df := parseFile(path)
fmt.Printf("Visited: %s [%x], %s, %s\n", df.FilePath)
}
return nil
}
func parseFile(path string)...
func check(e error) ...
func WalkDir(path string) {
err := filepath.Walk(path, visit)
fmt.Printf("filepath.Walk() returned %v\n", err)
}
答案1
得分: 6
将地图传递给返回filepath.WalkFunc
的函数,然后将其传递给filepath.Walk
。它类似于构造函数。
func visit(map Map) filepath.WalkFunc {
return func(path string, f os.FileInfo, err error) error {
// 使用地图做一些操作
fileName := f.Name()
if !f.IsDir() && strings.Contains(strings.ToLower(fileName), "myfile") {
df := parseFile(path)
fmt.Printf("Visited: %s [%x], %s, %s\n", df.FilePath)
}
return nil
}
}
func WalkDir(path string) {
err := filepath.Walk(path, visit(map))
fmt.Printf("filepath.Walk() returned %v\n", err)
}
英文:
Pass the Map to the function which returns a filepath.WalkFunc
and then pass it to filepath.Walk
. It is a constructor like thing.
func visit(map Map) filepath.WalkFunc {
return func(path string, f os.FileInfo, err error) error {
// do something with map
fileName := f.Name()
if !f.IsDir() && strings.Contains(strings.ToLower(fileName), "myfile") {
df := parseFile(path)
fmt.Printf("Visited: %s [%x], %s, %s\n", df.FilePath)
}
return nil
}
}
func WalkDir(path string) {
err := filepath.Walk(path, visit(map))
fmt.Printf("filepath.Walk() returned %v\n", err)
}
答案2
得分: 0
你可以这样写:
func (map Map) visit(path string, f os.FileInfo, err error) error {
// 对 map 进行操作
}
var mymap Map
err := filepath.Walk(path, mymap.visit) // mymap.visit 表示 'func(path string, f os.FileInfo, err error) error',与 visit 函数相同,只是将 mymap 作为接收者进行评估
这被称为 [方法值][1]
[1]: https://golang.org/ref/spec#Method_values
英文:
You can write
func (map Map) visit(path string, f os.FileInfo, err error) error {
...//Do something with map
}
var mymap Map
err := filepath.Walk(path, mymap.visit) //mymap.visit express 'func(path string, f os.FileInfo, err error) error' same as visit just with mymap evaluated
it's called method value
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论