英文:
How do I print that the type of a variable is interface {}
问题
你可以使用reflect.TypeOf
函数来打印j
的类型。以下是修改后的代码:
package main
import (
"fmt"
"reflect"
)
func main() {
fmt.Println("doing typeSwitchFunc(nil):")
typeSwitchFunc(nil)
fmt.Println("\ndoing typeSwitchFunc(22):")
typeSwitchFunc(22)
}
func typeSwitchFunc(i interface{}) {
switch j := i.(type) {
case nil:
fmt.Printf("case nil: j is type %T, j value %v, i is type %T, i value %v\n", j, j, i, i)
fmt.Printf("case nil: j is type: %v, j value: %v, j kind: %v\n", reflect.TypeOf(j), reflect.ValueOf(j), reflect.ValueOf(j).Kind())
default:
fmt.Printf("default: j is type %T, j value %v, i is type %T, i value %v\n", j, j, i, i)
fmt.Printf("default: j is type: %v, j value: %v, j kind: %v\n", reflect.TypeOf(j), reflect.ValueOf(j), reflect.ValueOf(j).Kind())
}
}
输出结果:
doing typeSwitchFunc(nil):
case nil: j is type <nil>, j value <nil>, i is type <nil>, i value <nil>
case nil: j is type: <nil>, j value: <invalid reflect.Value>, j kind: invalid
doing typeSwitchFunc(22):
default: j is type int, j value 22, i is type int, i value 22
default: j is type: int, j value: 22, j kind: int
你可以看到,在fmt.Printf
语句中使用%T
格式化动词来打印j
的类型。
英文:
package main
import (
"fmt"
"reflect"
)
func main() {
fmt.Println("doing typeSwitchFunc(nil):")
typeSwitchFunc(nil)
fmt.Println("\ndoing typeSwitchFunc(22):")
typeSwitchFunc(22)
}
func typeSwitchFunc(i interface{}) {
switch j := i.(type) {
case nil:
// How do I print the type of j as interface{} ? Below only gives me the information on underlying type stored in j -- (A)
fmt.Printf("case nil: j is type %T, j value %v, i is type %T, i value %v\n", j, j, i, i)
fmt.Printf("case nil: j is type: %v, j value: %v, j kind: %v\n", reflect.TypeOf(j), reflect.ValueOf(j), reflect.ValueOf(j).Kind())
default:
// How do I print the type of j as interface{} ? Below only gives me the information on underlying type stored in j -- (B)
fmt.Printf("default: j is type %T, j value %v, i is type %T, i value %v\n", j, j, i, i)
fmt.Printf("default: j is type: %v, j value: %v, j kind: %v\n", reflect.TypeOf(j), reflect.ValueOf(j), reflect.ValueOf(j).Kind())
}
}
output:
$ go run type-switch-minimal-example.go
doing typeSwitchFunc(nil):
case nil: j is type <nil>, j value <nil>, i is type <nil>, i value <nil>
case nil: j is type: <nil>, j value: <invalid reflect.Value>, j kind: invalid
doing typeSwitchFunc(22):
default: j is type int, j value 22, i is type int, i value 22
default: j is type: int, j value: 22, j kind: int
$ go version
go version go1.17.4 darwin/amd64
How do I print the type of j
as interface {}
?
答案1
得分: 4
如何打印 j 的类型作为 interface {}?
你无法直接打印 j 的类型为 interface {}。
来源:fmt
文档:
无论使用哪个占位符,如果操作数是一个接口值,将使用内部具体值,而不是接口本身。
你为什么对拼写 "interface{}" 感兴趣呢?fmt
包的这种行为使其能够立即与任何类型一起使用,而无需特殊的魔法。
在 @mkopriva 的示例中:
fmt.Println(reflect.TypeOf((*interface{})(nil)).Elem())
输出是 interface {}
,因为这是 *interface{}
元素类型的字符串表示,实际上是 interface {}
。但在这种情况下,你是通过反射来实现的,而不是使用 fmt 占位符。
英文:
> How do I print the type of j as interface {} ?
You can't.
Source: fmt
docs:
> Regardless of the verb, if an operand is an interface value, the internal concrete value is used, not the interface itself.
Why would you be interested in spelling out "interface{}" anyway? This behavior of the fmt
package is what allows it to work with any type right away without special magic.
In @mkopriva's example:
fmt.Println(reflect.TypeOf((*interface{})(nil)).Elem())
the output is interface {}
because that's the string representation of *interface{}
element type, which is truthfully interface {}
. But in this case you're getting there via reflection, not fmt verbs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论