Golang:environmentasmap.go 中的奇怪语法

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

Golang: weird syntax in environmentasmap.go

问题

我遇到了这段代码,并且其中两个变量和函数声明看起来非常奇怪:

getenvironment := func(data []string, getkeyval func(item string) (key, val string)) map[string]string {...}
environment := getenvironment(os.Environ(), func(item string) (key, val string) {

我大致理解代码的功能,但是这两行让我感到困惑。这样写真的可以吗?这些无名的(func(item string))和空的(getkeyval)函数是如何工作的?

英文:

I've stumbled upon this piece of code and two variables & function declarations from it look weird as hell:

getenvironment := func(data []string, getkeyval func(item string) (key, val string)) map[string]string {...}
environment := getenvironment(os.Environ(), func(item string) (key, val string) {

I understand what the code does in general, but these two lines just freak me out. Is it even okay to write like this? How do these unnamed (func(item string)) and empty (getkeyval) functions work?

答案1

得分: 1

getenvironment是一个函数变量,它是一个接受[]string和另一个函数getkeyval的函数。getkeyval函数预期获取一个string项,并返回key,val字符串值。getenvironment函数返回一个映射。

第二个代码片段调用该函数,并传递了一个匿名函数作为getkeyval的参数。

英文:

getenvironment is a function variable that is a function that takes a []string, and another function getkeyval. The getkeyval function is expected to get a string item, and return key,val string values. The getenvironment function returns a map.

The second snippet calls that function, passing an anonymous function for getkeyval.

答案2

得分: 1

第一行将一个函数字面量赋值给名为getenvironment的变量。调用该函数的第一个参数需要是[]string类型。第二个参数需要是func(string) (string, string)类型。

第二行调用了赋值给getenvironment的函数字面量,其中os.Environ()返回的值是[]string类型,而

func(item string) (key, val string) {
    splits := strings.Split(item, "=")
    key = splits[0]
    val = splits[1]
    return
}

func(string) (string, string)类型。

需要注意的是参数和返回值的名称不会影响函数的签名,它们仅用于文档化和提高代码可读性。

整个逻辑可以合并为一个函数声明,如下所示:

func getenvironment(data []string) map[string]string {
    items := make(map[string]string)
    for _, item := range data {
        splits := strings.Split(item, "=")
        key := splits[0]
        val := splits[1]
        items[key] = val
    }
    return items
}
英文:

The first line assigns a function literal to a variable called getenvironment. The first parameter of a call to that function needs to be of type []string. The second parameter needs to be of type func(string) (string, string).

The second line calls the function literal assigned to getenvironment, where the value returned by os.Environ() is of type []string and

func(item string) (key, val string) {
    splits := strings.Split(item, "=")
    key = splits[0]
    val = splits[1]
    return
}

is of type func(string) (string, string).

A thing to not get thrown off by here is the names of the parameters and return values. These don't affect a function's signature, and purely serve the purpose of documentation/code readability.

The whole logic could be collapsed into a single function declaration like so:

func getenvironment(data []string) map[string]string {
    items := make(map[string]string)
    for _, item := range data {
        splits := strings.Split(item, "=")
        key := splits[0]
        val := splits[1]
        items[key] = val
    }
    return items
}

huangapple
  • 本文由 发表于 2022年9月8日 04:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/73641367.html
匿名

发表评论

匿名网友

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

确定