gpt4 book ai didi

go - 将多返回函数的结果传递给另一个在 Go 中只接受一个参数的结果

转载 作者:IT王子 更新时间:2023-10-29 01:53:16 25 4
gpt4 key购买 nike

是否可以将返回多个值的结果形式函数直接传递给只接受一个值的函数?示例:

func MarshallCommandMap(mapToMarshall map[string]string) string {
return string(json.Marshal(mapToMarshall))
}

上面的例子会导致编译错误:multiple-value json.Marshal() in single-value context。我知道使用附加变量可以获得相同的结果:

func MarshallCommandMap(mapToMarshall map[string]string) string {
marshaledBytes, marshalingError := json.Marshal(mapToMarshall)
if (marshalingError != nil) {
panic(marshalingError)
}
return string(marshaledBytes)
}

但是是否可以不带任何变量直接传递第一个值?

最佳答案

我想你的意思是做一些像 python 的元组解包之类的事情。不幸的是,这在 Go (AFAIK) 中是不可能的。

关于go - 将多返回函数的结果传递给另一个在 Go 中只接受一个参数的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24250222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com