如何打印变量的类型为 interface {}?

huangapple go评论76阅读模式
英文:

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 (
	&quot;fmt&quot;
	&quot;reflect&quot;
)

func main() {

	fmt.Println(&quot;doing typeSwitchFunc(nil):&quot;)
	typeSwitchFunc(nil)

	fmt.Println(&quot;\ndoing typeSwitchFunc(22):&quot;)
	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(&quot;case nil: j is type %T, j value %v, i is type %T, i value %v\n&quot;, j, j, i, i)
		fmt.Printf(&quot;case nil: j is type: %v, j value: %v, j kind: %v\n&quot;, 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(&quot;default: j is type %T, j value %v, i is type %T, i value %v\n&quot;, j, j, i, i)
		fmt.Printf(&quot;default: j is type: %v, j value: %v, j kind: %v\n&quot;, 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 &lt;nil&gt;, j value &lt;nil&gt;, i is type &lt;nil&gt;, i value &lt;nil&gt;
case nil: j is type: &lt;nil&gt;, j value: &lt;invalid reflect.Value&gt;, 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.

huangapple
  • 本文由 发表于 2021年12月25日 15:43:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/70478628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定