英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论