当前位置:网站首页>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,无论用还是不用
边栏推荐
- L'avènement de l'ère 5G, une brève discussion sur la vie passée et présente des communications mobiles
- In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews
- 0基础如何学习自动化测试?按照这7步一步一步来学习就成功了
- In depth analysis of C language - variable error prone knowledge points # dry goods inventory #
- How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
- Custom classloader that breaks parental delegation
- SQL Yiwen get window function
- 近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
- 滴滴开源DELTA:AI开发者可轻松训练自然语言模型
- Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
猜你喜欢
高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
Jetpack's livedata extension mediatorlivedata
蓝桥杯单片机省赛第九届
The 5th Blue Bridge Cup single chip microcomputer provincial competition
集成底座方案演示说明
蓝桥杯单片机省赛第十一届第二场
Lost a few hairs, and finally learned - graph traversal -dfs and BFS
一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)
MySQL之账号管理
近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
随机推荐
Kotlin基础学习 15
跳出舒适区,5年点工转型自动化测试工程师,我只用了3个月时间
Gradle foundation | customize the plug-in and upload it to jitpack
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
0 foundation how to learn automated testing? Follow these seven steps step by step and you will succeed
Oracle common SQL
Basic operations of MySQL database (based on tables)
The fourth provincial competition of Bluebridge cup single chip microcomputer
Pandora IOT development board learning (HAL Library) - Experiment 2 buzzer experiment (learning notes)
Influence of air resistance on the trajectory of table tennis
蓝桥杯单片机省赛第五届
JS generate random numbers
SQL Yiwen get window function
Vite: configure IP access
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
VS2010插件NuGet
Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
In depth analysis of C language - variable error prone knowledge points # dry goods inventory #
整理了一份ECS夏日省钱秘籍,这次@老用户快来领走