GO常量与变量
作者:互联网
package main import ( "fmt" ) //单个变量(自行判断变量类型) var test1 = "content1" /*单个变量 先声明类型再赋值,此种方式函数中调用不了(会报错syntax error: non-declaration statement outside function body) 请再函数外设置 */ //var test2 string //test2 = "content2" //test3 : = "content3" //:=左侧的变量不应该是已经被声明过的,否则会导致编译错误 //单个变量 var test4 string = "content4" /*多个变量 此种方式函数中调用不了,请再函数外设置 */ //var test5, test6, test7 string //test5, test6, test7 = "content5", "content6", "content7" //test5, test6, test7 : = "content5", "content6", "content7" //多个变量 var test5, test6, test7 string = "content5", "content6", "content7" var test8, test9, test10 = "content8", "content9", "content10" //多个变量-类型不同(全局变量),局部变量不能使用该方法 var ( test11 string = "content11" test12 string = "content12" ) type person struct { name string age int } var P = person{"gxm", 20} //单个常量 const test13 string = "content13" const test14 = "content14" //多个常量 const test15, test16, test17 = "content15", "content16", "content17" //多个常量 const ( test18 string = "content18" test19 string = "content19" ) func main(){ //测试打印 fmt.Printf("%s%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s\n", "变量为:", test1,test4,test5,test6,test7,test8,test9,test10,test11,test12,P.name) fmt.Printf("%s%s-%s-%s-%s-%s-%s-%s", "常量为:", test13, test14, test15, test16, test17, test18, test19) }
标签:test5,常量,test7,test6,var,GO,变量,string 来源: https://blog.51cto.com/net881004/2487814