标签:数据类型 冲突 修改 作用域 浮点型 变量声明 pac 集合 全局
第一种,指明变量类型
var name type
name = value
//写在一行
var name int = 30
第二种,自行判断变量类型
var name = value
第三种,简短声明
name := 20
第四种,集合类型
//只声明
var (
name1 type1
name2 type2
)
//同时赋值
var (
name1 = 30
name2 = "dsafgsa"
)
常量是一个在程序运行时,不会被修改的量
const HELLO string = "hello world"
const PI = 3.14
const (
a = 1
b = 2
c = 3
)
iota是一个特殊的常量,可以认为是一个可以被编译器修改的常量
每当定义一个const,iota的初始值为0,每当定义一个常量,iota的值自动加一,直到下一个const出现,清零
iota可以用作枚举
package main
import "fmt"
func main() {
const (
a = iota //iota = 0
b //1
c //2
d = "hah" // iota = 3
e //hah iota=4
f = 100 //iota=5
g //100 iota=100
)
fmt.Println(a,b,c,d,e,f,g)
}
输出值
0 1 2 hah hah 100 100
int
int8
int16
int32
int64
uint8
uint16
uint32
uint64
byte //uint8
rune //int32
int //根据系统位数确认是int32,int64
float32
float64
//%f默认为小数点后6位
概念:多个byte的集合
单引号和双引号的区别
‘‘
默认类型为int32,用于单个字符
""
类型为string,用于字符串
标签:数据类型 冲突 修改 作用域 浮点型 变量声明 pac 集合 全局
原文地址:https://www.cnblogs.com/tomyyyyy/p/12896619.html