如何在Go中将Map传递给回调函数

huangapple go评论79阅读模式
英文:

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

huangapple
  • 本文由 发表于 2016年1月8日 03:54:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/34663992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定