多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
常量 1、常量使用const修饰,代表永远是只读,不能修改。 2、const只能修饰boolean,number(int相关类型,浮点类型。complex)和string 3、语法: const identifier [type] =value, 其中type可以省略 示例 const b string = "hello world" 字符串 const b = "hello world" 字符串 const b = 3.1415926 浮点数 const a = 9/3 表达式 4、优雅写法 const ( a=1 b=2 ) 5、简易写法,iota是将递增,每一个const开头,第一个项为0,在一个const中是递增的 const ( a=iota b c ) 则a=0 b=1 c=2 const ( d=iota e f ) 则d=0 e=1 f=2 当const中添加定义的数值时,递增被打断,不建议这么使用 const ( a=iota b c c1=1 c2 c3 ) 则a=0 b=1 c=2 c1=1 c2=1 c3=1 当使用左移符号位时,数值也是递增的左移 const ( a=1 << iota b c ) 则a=1 b=2 c=4 6、定于两个常量man=1和female=2,获取当前时间的秒数,如果能被female整除,则在终端打印female,否则打印man。 package main import ( "fmt" "time" ) const ( Man = 1 Female = 2 ) func main (){ now := time.Now() result := now.Unix() fmt.Printf("%v\n",result) if result % Female == 0 { fmt.Printf("man is :%v\n",Man) }else { fmt.Printf("female is :%v\n",Female) } } 变量 1、语法: var identifier type 示例: var a int var b int = 8 var c string = "hello" 或者 var ( a int //0 b string //"" c bool //false ) 空格是占用一个字符的空间,空串不占用