Go 空接口类型,存储任意类型数据_houyanhua1的专栏-CSDN博客 https://blog.csdn.net/houyanhua1/article/details/88712058

demo.go(空接口,存储任意类型数据):

package main

import "fmt"

func main() {
// 定义空接口类型变量 (可以接收任意类型的数据。相当于所有类型都实现了空接口)
var i interface{}
fmt.Printf("%T\n", i) // <nil>

i = 10 // int
fmt.Println(i) // 10
fmt.Printf("%T\n", i) // int
// int(i) + 20 // 会报错。 接口不能直接参与运算(类型转换),需要使用类型断言。

i = 3.14 // float64
fmt.Println(i) // 3.14
fmt.Printf("%T\n", i) // float64

i = "哈哈" // string
fmt.Println(i) // 哈哈
fmt.Printf("%T\n", i) // string
}

 

相关文章:

  • 2021-05-29
  • 2021-12-08
  • 2022-01-30
  • 2022-12-23
  • 2021-07-02
  • 2021-12-02
  • 2021-09-16
  • 2021-12-05
猜你喜欢
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
  • 2021-06-28
  • 2021-11-08
  • 2021-12-26
  • 2021-04-16
相关资源
相似解决方案