当前位置:网站首页>Type assertion of go interface variables
Type assertion of go interface variables
2022-07-25 17:39:00 【kankan231】
package main
import "fmt"
func main() {
var a interface{} = 1
// Variables participating in type assertions must be interface types , Otherwise, compilation error
switch a.(type) {//a.(type) It can only be written in switch In the sentence , Otherwise, compilation error
case float32:
fmt.Println(" A variable is float type ")
case int:// Will match this case
fmt.Println(" A variable is int type ")
default:
fmt.Println(" Variables are of other types ")
}
// Writing a , Security assertion
v1,ok:=a.(int)// Assert success
fmt.Printf("%v,%T,%v,%T",v1,v1,ok,ok)// Output 1,int,true,bool
v2,ok:=a.(*int)// Assertion failed
fmt.Printf("%v,%T,%v,%T",v2,v2,ok,ok)// Output <nil>,*int,false,bool
// Write two , Unsafe assertion
v3:=a.(int)// Assert success
fmt.Printf("%v,%T",v3,v3)// Output 1,int
v4:=a.(*int)// Assertion failed panic: interface conversion: interface {} is main.User, not *main.User
fmt.Printf("%v,%T",v4,v4)
}
边栏推荐
猜你喜欢

Redis源码与设计剖析 -- 17.Redis事件处理

什么是 IP SSL 证书,如何申请?

Stm32 paj7620u2 gesture recognition module (IIC communication) program source code explanation

Redis源码与设计剖析 -- 16.AOF持久化机制

I2C communication - sequence diagram

11. Camera and lens

Calculation date or date formatting

哈夫曼树的构建

面试官:说说 log.Fatal 和 panic 的区别

Starting from business needs, open the road of efficient IDC operation and maintenance
随机推荐
【PHP伪协议】源码读取、文件读写、任意php命令执行
Redis源码与设计剖析 -- 15.RDB持久化机制
论文阅读_多任务学习_MMoE
Methods of de duplication and connection query in MySQL database
我想理财,不懂,有没有保本金的理财产品?
Stm32 paj7620u2 gesture recognition module (IIC communication) program source code explanation
[solution] the Microsoft edge browser has the problem of "unable to access this page"
约瑟夫环问题
哈夫曼树的构建
【硬件工程师】元器件选型都不会?
Google Earth engine - download the globalmlbuildingfootprints vector collection of global buildings
Take you to a preliminary understanding of multiparty secure computing (MPC)
咨询下flink sql-client怎么处理DDL后,fink sql里面映射表加字段以及JOb?
生成扩散模型漫谈:DDPM = 贝叶斯 + 去噪
03. Longest substring without repeated characters
After consulting about how to deal with DDL in Flink SQL client, how to add fields and jobs to the mapping table in Fink SQL?
04. Find the median of two positive arrays
Random talk on generation diffusion model: DDPM = Bayesian + denoising
对灰度图像的三维函数显示
How to install govendor and open a project