This is a creation in Article, where the information may have evolved or changed.
1. Variables
1)变量声明 格式:var 变量名 变量类型
varV1int varV2string varV3[Ten]int //integer array varV4 []int //Slice varV5struct{//StructureFint}varV6 *int //Hands varV7Map[string]int //map,key is a string type and value is of type int varV8func(Aint)int var(//multiple var omitted as aV1intV2string)
2)变量初始化 格式1:变量名 = 值 //注:此处的v1必须是已声明的
v1 = 10
格式2:变量名 := 值 //注:此处的V1必须是未声明的
v1 := 10
格式3:var 变量名 = 值 //注:该格式为声明时直接赋值
var v1 = 10
3)变量赋值 格式:变量名 = 值
v1 = 10
这里介绍下Go语言的一个小特性
i,j = j,i
上面语句的意思为将i与j的值对调,如果在Java中实现该赋值,则需要引入临时变量
4)匿名变量 匿名变量一般在多返回值函数时使用,当调用一个多返回值函数时,为了只获取一个值, 又不想给每个返回值都定义一个变量名,此时就可以使用匿名变量,使用“_”来进行占位
ret1,_:= MutipleReturnFunc()
2. Constants
1)字面常量 所谓字面常量是指程序中硬编码的常量
-12 3.1415926 true "foo"
2) Constant definition Format: const constant name [Type] [= constant value]//[] means that there can be no such content
const Pi float64 = 3.1415926 cons T Zero = 0.0 const (size int64 = 1024x768 eof =-1 ) const u,v float64 = 0 ,3 //u=0.0,v=3.0 constant multiple assignment const mask = 1 << 3 //compile-time behavior expression, cannot be a run-time expression
3)预定义常量 Go言预定义了三个常量:true、false和iota iota比较特殊,可以被认为是一个可被编译器个性的常量,在每个const关键字出现时被重置为0, 在下个const出现之前,每出现一次iota其所代表的数字会自动增1
Const(Byte =1<<(Ten*Iota)//byte = 1KB =1<<(Ten*Iota)//kb = 1024x768MB =1<<(Ten*Iota)//MB = 1048576GB =1<<(Ten*Iota)//GB = 1073741824)ConstV1 =Iota the//0,iota is reset to 0 again.
注:这里有一个小特性,在Go中,如果在同一个const第一个常量在定义时进行了赋值, 而后续其它常量没有进行赋值,则后续常量将延用第一个赋的值或表达式 如下面的定义语句等同于上面的语句
const ( Byte = 1(10iota) //Byte = 1 KB //KB = 1024 MB //MB = 1048576 GB //GB = 1073741824 )
4)枚举 Go语言工不支持enum关键字来定义枚举,因此Go语言的枚举其实只是一系列相关常量
const ( iota Monday Tuesday Wednesday Thursday Friday Saturday )
3. Data type (base)
The data type of the Go language consists of the following basic types and composite types (which will be explained in a separate article blog post)
基础类型: 1)布尔类型:bool,不支持其它类型的转换(除使用函数外) 2)整型 int8 :[-128,127] int16 :[-32768,32767] int32 :[-2 147 483 648,2 147 483 647] int64 :[-9 223 372 036 854 775 808,9 223 372 036 854 775 807] int :依赖不同平台下的实现,可以是int32(大小)或int64(大小),自动推导 uint8 :又名byte,[0,255] uint16 :[0,65535] uint32 :又名rune,[0,4 294 967 295] uint64 :[0,18 446 744 073 709 551 615] uint :依赖不同平台下的实现,可以是uint32(大小)或uint64(大小) uintprt:恰好容纳指针值的类型,对32位平台是unit32,对64位平台是unit64 注:不同类型的整型被认为是不同类型,它们之间无法做赋值、比较运算。 在Go语言中,没有++与--操作符,仅有"值++"与"值--"语法 如下面的写法为错误写法。
varint32 v2 := 64 //v2被自动推导为int类型 v1 = v2 //不管为几位系统均编译错误 int32(v2) //显示类型转换 equal := (v1 == v2) //编译错误 v1++ //正确,中间不能有空格 ++v1 //编译错误
3)浮点型:float32 float64(自动推导) 同整型一样,不同类型的浮点型是被认为是不同类型他们之间无法做赋值、比较运算。 因为浮点数不是一种精确的表达方式(计算机为二进制,二进制无法准确的表示小数), 所以如果直接使用==判断两个浮点数是否相等可能会导致不稳定的结果,这里给出一个替代方案
varfloat64 = 3.14, 3.14 fmt.Println(v1 == v2) //在这里,本行打印为true fmt.Println(math.Dim(v1, v2) < 0.01) //0.01表示精度,本行打印为true
4)字符串类型:string 使用""(双引号,转义符会被转义)或者``(反引号,字符保持不变)来创建 字符串类型支持"+="及"+"操作符进行这符串连接
STR1: ="Hello" //String Assignmentstr ="Hello\nworld" //Print STR, there will be a newlinestr =' Hello\nworld ' //Print results Hello\nworldFirstchar: = str1[0]//Take the first character of the stringStr1[0] =' h ' //Compile error, the contents of the string cannot be modified after initializationSTR: = str1 +"World" //Character connection operationStrLen: = Lent (str)//String length, here the value is oneSTRCN: ="Hello, the World" //String containing ChineseStrcnlen: = Lent (STRCN)//String length, where the value is 12, in UTF-8, a Chinese account of three bytesstr + ="Appendstr" //Append stringstr ="Hello"+"World" //Stitching stringsstr ="Hello"+"World" //+ cannot be placed at the beginning of the linestr ="Hello"+"World" //Compilation errorSB: = bytes. buffer{}//Use buffer splicingSb. WriteString ("Hello") sb. WriteString ("World") str: = sb. String ()
5)字符类型: 使用''(单引号)创建 byte,实际上是uint8的别名,代表UTF-8字符串的单个字节值 rune,代表单个Unicode字符 6)类型的零值(变量被声明为某种类型后的默认值) 通常情况下,数值类型的默认值为0,bool为false,string为空字符串 7)类型转换 Go语言不支持隐式转换,所有类型转换必须显式声明,Go是一个类型安全的编程语言 转换只有发生在两种相互兼容的类型之间 格式:ValueA [:]= TypeOfValueA(ValueB)
varfloat64 = 1.1 int(a) //会发生精度损失 varbool false int(c) //编译错误
这里在讲一下比较常见的int->string,string->int,这个无法直接转换,需要借助strconv包
str := strconv.Itoa(intV) intV := strconv.Atoi(str)
At this point, the basic types and use have been explained, the follow-up will have other Go Language blog post.
We also recommend a foreign language translation, the Java Programmer Quick Start Go language (English text: Go for Java programmers), this article is a good article for the Java programmer to get started with the go language.
No smell (Unknow) "Go Programming Basics"
Xu Xiwei's "Go Language Programming"