在Golang上返回像普通函数中的'ok'一样的地图 [英] Return map like 'ok' in Golang on normal functions
问题描述
pre $ $ $ $ $ package main
$ b importfmt
var someMap = map [string] string {some key:hello}
func main (){
if value,ok:= someMap [some key]; OK {
fmt.Println(value)
}
value:= someMap [some key]
fmt.Println(value)
}
然而,我不知道如何用我自己的函数来做同样的事情。是否有可能具有类似的行为,如 map ?
的可选返回值例如:
package main
importfmt
func Hello()(string,bool) {
returnhello,true
}
func main(){
if value,ok:= Hello();好的{
fmt.Println(价值)
}
价值:=你好()
fmt.Println(价值)
}
不会编译(由于单值上下文中多值Hello() / code>)...有没有办法使这个语法适用于函数
Hello()
?
map
元素的两种形式由 Go语言规范:索引表达式。
使用函数不能执行此操作。如果一个函数有2个返回值,你必须期待他们两个或根本没有。
然而,您可以将任何返回值分配给空白标识符:
s,b:= Hello()//存储两个返回值
s2,_:= Hello()//只存储第一个
_,b3:= Hello()//只存储第二个
您也可以不选择以存储任何返回值:
Hello()//只执行它,但不存储任何返回值
注意:您也可以将两个返回值分配给空白标识符,尽管它没有用处比验证它有2个返回值):
_,_ = Hello()//不存储任何返回值;请注意=而不是:=
您也可以在 Go Playground 。
助手功能
如果您多次使用它并且不想使用空白标识符,请创建一个辅助函数,该函数会丢弃第二个返回值:
func Hello2()string {
s,_:= Hello()
return s
}
现在您可以这样做:
value:= Hello2()
fmt.Println(value)
In Go, the following works (note one use of the map has one return, the other has two returns)
package main
import "fmt"
var someMap = map[string]string { "some key": "hello" }
func main() {
if value, ok := someMap["some key"]; ok {
fmt.Println(value)
}
value := someMap["some key"]
fmt.Println(value)
}
However, I have no idea how to do this same thing with my own function. Is it possible to have similar behavior with an optional return like map
?
For example:
package main
import "fmt"
func Hello() (string, bool) {
return "hello", true
}
func main() {
if value, ok := Hello(); ok {
fmt.Println(value)
}
value := Hello()
fmt.Println(value)
}
Wont compile (due to the error multiple-value Hello() in single-value context
) ... is there a way to make this syntax work for the function Hello()
?
map
is different because it is a built-in type and not a function. The 2 forms of accessing an element of a map
is specified by the Go Language Specification: Index Expressions.
With functions you can't do this. If a function has 2 return values, you have to "expect" both of them or none at all.
However you are allowed to assign any of the return values to the Blank identifier:
s, b := Hello() // Storing both of the return values
s2, _ := Hello() // Storing only the first
_, b3 := Hello() // Storing only the second
You can also choose not to store any of the return values:
Hello() // Just executing it, but storing none of the return values
Note: you could also assign both of the return values to the blank identifier, although it has no use (other than validating that it has exactly 2 return values):
_, _ = Hello() // Storing none of the return values; note the = instead of :=
You can also try these on the Go Playground.
Helper function
If you use it many times and you don't want to use the blank identifier, create a helper function which discards the 2nd return value:
func Hello2() string {
s, _ := Hello()
return s
}
And now you can do:
value := Hello2()
fmt.Println(value)
这篇关于在Golang上返回像普通函数中的'ok'一样的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!