当前位置:网站首页>go 变量与常量
go 变量与常量
2022-07-02 03:45:00 【UPythonFish】
go 变量与常量
变量是什么
变量指定了某存储单元(Memory Location)的名称,该存储单元会存储特定类型的值。在 Go 中,有多种语法用于声明变量。
变量定义的4种方式
func main() {
// 1. 完整定义 var 变量名 变量类型 = 变量值
var name string = "yxh"
var age int = 20
fmt.Println(name,age)
// 2. 类型推导 var 变量名 = 变量值
var name = "yxh"
var age = 20
fmt.Println(name,age)
// 3. 简略声明 变量名 := 变量值
name := "yxh"
age := 20
fmt.Println(name,age)
// 4. 同时定义多个变量
// 4.1 使用一行定义
var name, gender string
var name, age, gender = "yxh", 20, "男"
name, age, gender := "yxh", 20, "男" // 推荐使用
// 4,2 了解
var (
name = "yxh"
age = 20
gender = "男"
)
var (
name string
age int
gender string
)
fmt.Println(name,age,gender)
}
变量定义注意事项:
1. 变量声明类型后,只能使用同类型的值对其进行修改
2. 简短声明只能在函数内部使用,外部使用不了
3. 变量要先定义,在使用,并且只能定义一次,可以只定义不赋值
var age =19 // 定义了一个变量age
name,age :="lqz",20 // 定义了变量name,可以写age,表示使用,修改值,不是定义了,除此以外,其他情况都不行
4. 变量声明了必须使用,否则会报错
5. 只定义不赋值,只能使用完整定义方案
变量的类型
// 基础数据类类型
-数字 后面数字代表一个数字占多少个bit,和范围大小
-整型
-int // 分平台,在32位机器,表示int32,在64位机器表示int64
-int8 // 占8个bit,数字范围为 ±2的7次方减1 -128 —— 127
-int16 // 正负2的15次方-1
-int32 // 正负2的31次方-1
-int64 // 正负2的63次方-1
-正整数:无符号整型
-uint // 在32位机器是 uint32,在64位机器是uint64
-uint8 // 占8个bit,数字范围为 2的8次方-1 0 —— 256
-uint16 // 2的16次方-1
-uint32 // 2的32次方-1
-uint64 // 2的63次方-1
-浮点型
-float32 // 精确到小数点后6位
-float64 // 精确到小数点后15为
-复数(正常开发中用不到)
-complex128
-complex64
-字符串
-string
-双引号" "或 反引号` `包裹 反引号表示多行字符串,支持换行
单引号不行:单引号表示数字,并且单引号里面只能放一个字符
fmt.Println('a') // 97 a字符的Unciode编码
fmt.Println('你') // 20320 你字符的Unicode编码
-布尔
-bool
-true false (都是小写)
-byte:uint8 的别名
-rune:int32 的别名
// 复合数据类型
-数组
-切片
-map
// 高级数据类型
-自定义的类型
-函数类型
-指针类型
// 查看数据类型两个方法
fmt.Println(reflect.TypeOf(nameOfYxh))
fmt.Printf("%T", nameOfYxh) // 这个只能在 fmt.Printf()中使用
类型转换
Go 有着非常严格的强类型特征。Go 没有自动类型提升或类型转换。需要进行显示转换才能进行对应操作,列如
package main
import (
"fmt"
)
func main() {
i := 55 // int
j := 67.8 // float64
sum := i + j // 不允许 int + float64
sum := i + int(j) // 允许
j = "yxh" // 不允许,报错
fmt.Println(sum)
}
补充: 在python中 int , float等不是基础数据类型,而是一个个对象,并且变量名指向的是一个个内存地址,而不是值,所以python中变量名命名简单,并且类型转换也很方便
常量和iota
常量, 恒定不变的量,在go中一旦定义赋值后,再也不能改变了, 而在python中通常以全大写的变量来设为常量
常量的定义和使用
package main
import "fmt"
func main() {
// 1 常量的定义和使用
// 常量定义格式 const关键字 常量名 类型 = 值 或者 const关键字 常量名 = 值
const name string= "yxh"
// 不允许修改
name="lxx" // 报错
fmt.Printf(name)
// 同时定义多个常量
const name ,age= "yxh",20
const (
name ="yxh"
age=20
)
// 2 iota:自增常量
const (
a =1
b // 1 如果不写赋值,遵从上面的赋值 相当于 b=1
c // 1
d // 1
)
fmt.Println(a,b,c,d) // 1 1 1 1
const (
a = 1
b // 1 如果不写赋值,遵从上面的赋值 相当于 b=1
c = 99
d // d = 99
)
fmt.Println(a,b,c,d) // 1 1 99 99
const (
a =iota // 等同于a=0
b =iota // 自增
c =iota // 自增
d =iota // 自增
)
fmt.Println(a,b,c,d) // 0 1 2 3
const (
a =iota
b =100
c
d
e =iota // 每一行都会自增
)
fmt.Println(a,b,c,d,e) // 0 100 100 100 4
const (
e, f, g = iota, iota, iota // 只要不换行,就不自增
h = iota
r = iota
)
fmt.Println(e, f, g, h, r) // 0 0 0 1 2
const (
a = iota + 1
b
c
)
fmt.Println(a, b, c) // 1 2 3
const (
a = 4 // 显式的指定值
b = 5 // 显式的指定值
c = iota // c = 2,因为这里的 iota 位于第3个ConstSpec,2=3-1
d = iota // d = 3,因为iota递增了1,等价于 d = iota
)
fmt.Println(a, b, c, d) // 4 5 2 3
const (
a =0 // a = 0
d=1
_ // 跳过这个ConstSpc,但是iota会加1
b, c = iota, iota * 2 // iota=2
)
fmt.Println(a,b,c,d) // 0 3 6 1
// 同时定义多个常量,如果下一行只写常量名,遵从上一行的赋值方式
// iota每换一行,就会自增1,无论用还是不用
边栏推荐
- Kotlin basic learning 15
- C语言:逻辑运算和判断选择结构例题
- Introduction to Robotics II. Forward kinematics, MDH method
- 蓝桥杯单片机省赛第七届
- JS generate random numbers
- Unity脚本的基础语法(6)-特定文件夹
- [punch in] flip the string (simple)
- Jetpack's livedata extension mediatorlivedata
- 【小技巧】使用matlab GUI以对话框模式读取文件
- Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
猜你喜欢
![[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network](/img/11/4a8b52603e6e14a1ed6da1264dee57.png)
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network

蓝桥杯单片机省赛第七届

Kubernetes cluster storageclass persistent storage resource core concept and use

Lost a few hairs, and finally learned - graph traversal -dfs and BFS

Learn more about materialapp and common attribute parsing in fluent

蓝桥杯单片机省赛第十二届第二场

The 8th Blue Bridge Cup single chip microcomputer provincial competition

MySQL之账号管理

软件测试人的第一个实战项目:web端(视频教程+文档+用例库)

蓝桥杯单片机省赛第十一届第一场
随机推荐
Introduction to Robotics II. Forward kinematics, MDH method
蓝桥杯单片机省赛第十一届第二场
BiShe cinema ticket purchasing system based on SSM
5G时代全面到来,浅谈移动通信的前世今生
5G時代全面到來,淺談移動通信的前世今生
The 11th Blue Bridge Cup single chip microcomputer provincial competition
PY3, PIP appears when installing the library, warning: ignoring invalid distribution -ip
Influence of air resistance on the trajectory of table tennis
Getting started with MQ
Oracle viewing locked tables and unlocking
JS generate random numbers
PY3 link MySQL
Oracle common SQL
Kotlin basic learning 17
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
毕设-基于SSM电影院购票系统
《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真
C语言:逻辑运算和判断选择结构例题
Analyse de 43 cas de réseaux neuronaux MATLAB: Chapitre 42 opérations parallèles et réseaux neuronaux - - opérations parallèles de réseaux neuronaux basées sur CPU / GPU
SQL Yiwen get window function