当前位置:网站首页>【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)}
- &, 取地址操作符,功能是取出变量在内存中的地址;
- *,取值操作符,功能是取出地址指向的实际值.
边栏推荐
- Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
- ELK部署脚本---亲测可用
- Shell programming conditional statement test command Integer value, string comparison Logical test File test
- What are the efficient open source artifacts of VSCode
- Axure Carousel
- 寄存器(汇编语言)
- 【深入浅出玩转FPGA学习13-----------测试用例设计1】
- Asser uses ant sword to log in
- Point Cloud Scene Reconstruction with Depth Estimation
- C语言力扣第48题之旋转图像。辅助数组
猜你喜欢

正则表达式密码策略与正则回溯机制绕过

ES 中时间日期类型 “yyyy-MM-dd HHmmss” 的完全避坑指南

Homework: iptables prevent nmap scan and binlog

Gabor滤波器学习笔记

How to solve the error of joiplay simulator

Oracle一个诡异的临时表空间不足的问题

会议OA项目待开会议、所有会议功能

firewalld

Android security optimization - APP reinforcement
How to ensure the consistency of database and cache data?
随机推荐
46.
MySQL grant statements
redis学习
MySQL notes under
过滤器(Filter)
Error occurred while trying to proxy request项目突然起不来了
Add text watermark to PHP image
firewalld
pytorch bilinear interpolation
joiplay模拟器rtp如何安装
Gabor滤波器学习笔记
常用的正则表达式
[In-depth and easy-to-follow FPGA learning 14----------Test case design 2]
封装、获取系统用户信息、角色及权限控制
消息队列存储消息数据的MySQL表设计
What are the efficient open source artifacts of VSCode
【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
The difference between truncate and delete in MySQL database
Kotlin协程:协程上下文与上下文元素
In-depth understanding of the auto-increment operator from two error-prone written test questions