当前位置:网站首页>【Yugong Series】July 2022 Go Teaching Course 013-Constants, Pointers
【Yugong Series】July 2022 Go Teaching Course 013-Constants, Pointers
2022-07-31 00:23:00 【HUAWEI CLOUD】
一、常量
1.常量的定义
常量是一个简单值的标识符,在程序运行时,不会被修改的量.
常量中的数据类型只可以是布尔型、数字型(整数型、浮点型和复数)和字符串型.
常量的定义格式:
const identifier [type] = value你可以省略类型说明符 [type],因为编译器可以根据变量的值来推断其类型.
定义一个 Go 语言常量非常简单,它和变量的声明方式类似,将 var 关键字替换成 const 即可,代码如下:
const pi = 3.141592const name = "愚公搬代码"2.常量的使用
package mainimport ( "fmt")const ( pi = 3.141592 name = "愚公搬代码")func main() { fmt.Println(pi, name)}
二、指针
1.指针的定义
在计算机中,所有的数据都是存放在存储器中的,不同的数据类型占有的内存空间的大小各不相同.内存是以字节为单位的连续编址空间,每一个字节单元对应着一个独一的编号,这个编号被称为内存单元的地址.比如:int 类型占 4 个字节,char 类型占 1 个字节等.系统在内存中,为变量分配存储空间的首个字节单元的地址,称之为该变量的地址.地址用来标识每一个存储单元,方便用户对存储单元中的数据进行正确的访问.在高级语言中地址形象地称为指针.
2.指针的使用
每个变量在程序运行时都有一个地址,这个地址代表变量在内存中的位置.
Go 语言中,通过 & 操作符对变量进行 “取地址” 操作,格式如下:
p := &v // v 的类型为 T上面代码中,v 表示被取地址的变量,取到的地址用变量 p 进行接收, p 的类型为 “*T”, 称为 T 的指针类型.*代表指针.
案例如下:
package mainimport "fmt"func main() { var name string = "愚公搬代码" // 获取字符串的指针, p 类型为*string p := &name // 打印变量 p 的类型 fmt.Printf("p type: %T\n", p) // 打印变量 p 的指针地址 fmt.Printf("address: %p\n", p) // 通过 * 对指针进行取值操作 value := *p // 取值后的类型 fmt.Printf("value type: %T\n", value) // 指针取值后指向变量的值 fmt.Printf("value type: %s\n", value)}
- &, 取地址操作符,功能是取出变量在内存中的地址;
- *,取值操作符,功能是取出地址指向的实际值.
边栏推荐
- The first level must project independently
- 【愚公系列】2022年07月 Go教学课程 016-运算符之逻辑运算符和其他运算符
- 【深入浅出玩转FPGA学习13-----------测试用例设计1】
- Word文件损坏如何修复
- Gabor filter study notes
- joiplay模拟器如何调中文
- 网络常用的状态码
- Add text watermark to PHP image
- MySQL notes under
- Ukraine's foreign ministry: wu was restored to complete the export of food security
猜你喜欢

Learn Scope from a Compilation Perspective!

How to import game archives in joiplay emulator

mysql索引失效的常见9种原因详解

Steven Giesel recently published a 5-part series documenting his first experience building an application with the Uno Platform.

C language force buckles the rotating image of the 48th question.auxiliary array

Homework: iptables prevent nmap scan and binlog

WEB安全基础 - - -漏洞扫描器

从笔试包装类型的11个常见判断是否相等的例子理解:包装类型、自动装箱与拆箱的原理、装箱拆箱的发生时机、包装类型的常量池技术

从两个易错的笔试题深入理解自增运算符

.NET 跨平台应用开发动手教程 |用 Uno Platform 构建一个 Kanban-style Todo App
随机推荐
Shell script if statement
消息队列存储消息数据的MySQL表设计
【Yugong Series】July 2022 Go Teaching Course 019-For Circular Structure
How to use joiplay emulator
Summary of the stock problem of state machine dynamic programming
h264和h265解码上的区别
In-depth understanding of the auto-increment operator from two error-prone written test questions
如何在WordPress网站上添加导航菜单
How to ensure the consistency of database and cache data?
Error occurred while trying to proxy request The project suddenly can't get up
A Brief Talk About MPI
会议OA项目待开会议、所有会议功能
what is jira
ABC 261 F - Sorting Color Balls(逆序对)
(五)fastai应用
【深入浅出玩转FPGA学习15----------时序分析基础】
MySQL筑基篇之增删改查
binglog日志追踪:数据备份并备份追踪
The difference between substring and substr in MySQL
pytorch双线性插值