go 学习2 const

package main

import "fmt"

func main(){
	/*
	常量:
	1.概念:同变量类似,程序执行过程中数值不能改变
	2.语法:
		显式类型定义
		隐式类型定义
	3.常数
		固定的数值:100,“abc”
	 */

	const path string = "100"
	const pi = 3.14

	const c1,c2,c3 = 100,3.14,"hah"
	const (
		male = 0
		female = 1
		unknow = 3)

	const (
		a int = 100
		b
		c string = "man"
		d
		e
	)
	fmt.Printf("%T,%d\n",a,a)
	fmt.Printf("%T,%d\n",a,b)
	fmt.Printf("%T,%d\n",a,c)
	fmt.Printf("%T,%d\n",a,d)
	fmt.Printf("%T,%d\n",a,e)

	const(
		SPRING=0
		SUMMER=1
		AUTUME=2
		WINTER=3
	)

}
GOROOT=D:\Go #gosetup
GOPATH=D:\GO_WorkSpace #gosetup
D:\Go\bin\go.exe build -o C:\Users\lenovo\AppData\Local\Temp\___go_build_awesomeProject_src_hello__1_.exe D:/GO_WorkSpace/src/awesomeProject/src/hello/const1.go #gosetup
C:\Users\lenovo\AppData\Local\Temp\___go_build_awesomeProject_src_hello__1_.exe #gosetup
int,100
int,100
int,%!d(string=man)
int,%!d(string=man)
int,%!d(string=man)

package main

import "fmt"

func main(){
	/*
	iota:特殊的变量,可以被编译器自动修改的变量
		每当定义一个const,iota的初始值为0
		每当定义一个常量,就会累加1
		直到下一个const出现清零
	 */
	const(
		a=iota
		b=iota
		c=iota
	)
	fmt.Println(a)
	fmt.Println(b)
	fmt.Println(c)

	const(
		d=iota
		e
	)
	fmt.Println(d)
	fmt.Println(e)

	const(
		male=iota
		female
		unknow
	)
	fmt.Println(male,female,unknow)

	const (
		one=iota //0
		two //1
		three //2
		four="ma" //iota = 3
		five  //iota = 4
		six=100 //iota = 5
		seven //iota = 6
		eight=iota //7
		nine //iota 8
	)
	const(
		ten=iota  //0
	)
	fmt.Println(one)
	fmt.Println(two)
	fmt.Println(three)
	fmt.Println(four)
	fmt.Println(five)
	fmt.Println(six)
	fmt.Println(seven)
	fmt.Println(eight)
	fmt.Println(nine)
	fmt.Println(ten)
}
0
1
2
0
1
0 1 2
0
1
2
ma
ma
100
100
7
8
0

发布了57 篇原创文章 · 获赞 59 · 访问量 9694

猜你喜欢

转载自blog.csdn.net/AI_LINNGLONG/article/details/105231173