Golang 中的静态类型和动态类型

news/2025/2/16 5:50:54/

定义说明

  • 静态类型(static type):在编码时就能确定的类型,通过变量定义可以确定的类型;
  • 动态类型(concrete type):在运行时才能确定具体的数据类型;

动态静态类型如何理解?

go 语言中interface(any)可以承接所有类型的数据,所以这部分只有具体运行的时候,才能确定数据具体类型:

	var i interface{}var num int = 1var str string = "hello world"i = numi= str

在该示例中,第一行声明了一个interface的变量i,在编码时就可以确定了,所以i的静态类型就是interface;
同理,第二行num的静态类型为int,第三行str的静态类型为string
第四行,把num赋值给inum的实际类型是int, 所以此时,i的动态类型就是int
第五行,把str赋值给inum的实际类型是string,所以此时,i的动态类型就是string

Golang 中的interface的底层延申:

golang 中interface有两种含义/用法:

  1. 常规的接口类型,有一些带实现的接口定义
  2. 表示任意数据类型any

golang 的底层实现也是根据这两种情况做了不同的实现,底层分别对应ifaceeface

// 空接口结构
type eface struct {_type *_type			// 实体类型data  unsafe.Pointer	// 数据地址
}// 包含方法的结构
type iface struct {tab  *itab			// 接口和实体类型data unsafe.Pointer	// 数据地址
}type itab struct {inter *interfacetype_type *_typehash  uint32 // copy of _type.hash. Used for type switches._     [4]bytefun   [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
}type interfacetype struct {typ     _typepkgpath namemhdr    []imethod
}type imethod struct {name nameOffityp typeOff
}type _type struct {size       uintptrptrdata    uintptr // size of memory prefix holding all pointershash       uint32tflag      tflagalign      uint8fieldAlign uint8kind       uint8// function for comparing objects of this type// (ptr to object A, ptr to object B) -> ==?equal func(unsafe.Pointer, unsafe.Pointer) bool// gcdata stores the GC type data for the garbage collector.// If the KindGCProg bit is set in kind, gcdata is a GC program.// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.gcdata    *bytestr       nameOffptrToThis typeOff
}type nameOff int32
type typeOff int32
type tflag uint8

http://www.ppmy.cn/news/1090477.html

相关文章

【数字通信原理】笔记(持续更新ing)

通信原理学习笔记,课程见b站: 由于教材不同,我们的课程使用的是《数字通信原理》主编:李白萍 版本,因此此笔记以我们的教材为主整理up主的笔记。 详情见:通信原理 文章目录 第一章 绪论1. 通信的基本概念2. 信息的量度3. 通信系统的性能指标 …

Jira 笔记

目录 1. Jira 笔记1.1. 项目管理工具 JIRA 实践指导1.2. JIRA 1. Jira 笔记 1.1. 项目管理工具 JIRA 实践指导 https://zhuanlan.zhihu.com/p/619453520?utm_id0 1、JIRA 序言篇1.1 为什么要使用项目管理工具1.2 项目管理工具分析与比较二 JIRA 配置篇2.1 JIRA 配置之问题类…

【运维基础】文本编辑器---nano的使用

前言 Nano 是一个简单易用的命令行文本编辑器,下面是一些基本使用方法 文章目录 前言打开文件光标控制:保存和退出: 打开文件 你可以使用以下命令打开一个文件进行编辑: nano 文件名光标控制: 使用方向键&#xf…

第三方软件检测机构资质要求有哪些?专业测试报告如何申请?

科技信息的快速发展使得人们对于软件产品极度依赖,因此要想保障产品质量,测试必不可少。作为一家合格的软件检测机构,应当严格遵守相关资质要求,保证测试报告的专业性和可信度。 一、第三方软件检测机构需要具备的资质 1. 认证资…

TypeScript DOM类型的声明

TS DOM类型的声明 lib.dom.d.ts HTMLInputElement <input type"text" change"handleChange" /> const handleChange (evt: Event) > {console.log((evt.target as HTMLInputElement).value); } HTMLElement const div: HTMLDivElement do…

C++std::function和std::bind()的概念

std::function&#xff1a; 一个通用的函数封装器&#xff0c;它允许你存储和调用任何可以被调用的东西&#xff0c;例如函数、函数指针、函数对象、Lambda 表达式等。 std::bind&#xff1a; 用于创建函数对象。一个可调用对象的绑定版本&#xff0c;可以提前绑定某些参数&am…

Alibaba Canal 使用记录

项目中使用 canal 来同步数据到 Elasticsearch, 遇到很多问题&#xff0c;做一下记录&#xff1a; 版本问题&#xff1a; 1. 解析binlog出错 &#xff0c;表现为 limit excceed&#xff1a;xx 目前使用 mariadb 10.9.7/10.10.6 canal 1.1.6 hotfix &#xff0c;在这个版本组…

华为OD机试 - 关联子串 - 滑动窗口(Java 2023 B卷 100分)

目录 专栏导读一、题目描述二、输入描述三、输出描述四、解题思路五、Java算法源码六、效果展示1、输入2、输出3、说明4、换个思路 华为OD机试 2023B卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试&#xff08;JAVA&#xff09;真题&#xff08;A…