小编Fel*_*tem的帖子

如何确定函数返回的值的数量

如何获取函数的返回值长度,效果类似如下:

func Foo() (int, int){
    ...
}
func Bar() (int, float, bool, string, error){
    ...
}
n1 := getReturnLength(Foo) // shall be 2
n2 := getReturnLength(Bar) // shall be 5
Run Code Online (Sandbox Code Playgroud)

我该如何实现getReturnLength功能或平等的功能?

reflection go

0
推荐指数
1
解决办法
101
查看次数

go map value 和 nil 之间的区别

在 go 中,当 key 不存在时,map 的值是零值。我在下面有一个简短的代码片段: 操场

package main

import (
    "sync"
)

func main() {
    var mm map[int]sync.Mutex
    var m sync.Mutex
    mm[1].Lock() // not work due to cannot call pointer method on mm[1] and cannot take the address of mm[1]
    m.Lock() // work normal
}
Run Code Online (Sandbox Code Playgroud)

mm[1]m上面有什么区别?我用reflect来检查,但看不出它们之间的区别。关于导致差异的任何线索?

go

-2
推荐指数
1
解决办法
88
查看次数

标签 统计

go ×2

reflection ×1