Golang学习笔记:语言规范之词汇元素和类型

Golang学习笔记:语言规范之词汇元素和类型,第1张

概述引言 Golang参考手册,更多内容请参考http://golang.org 语言规范参考自https://golang.org/ref/spec 词汇元素 注释 行注释: // comment content 段注释:/* comment content */ 符号 符号有4类:标识符、关键字、 *** 作符和分隔符、字面常量 分号 golang中,分号可省略 标识符 标识符即程序中的变量和类型,有一个 引言

Golang参考手册,更多内容请参考http://golang.org
语言规范参考自https://golang.org/ref/spec

词汇元素 注释

行注释: // comment content
段注释:/* comment content */

符号

符号有4类:标识符、关键字、 *** 作符和分隔符、字面常量

分号

golang中,分号可省略

标识符

标识符即程序中的变量和类型,有一个或多个字母或数字组成,第一个字母必须字母开头。

IDentifIEr = letter { letter | unicode_digit }
关键字

标识符不能是golang中的保留关键字

break        default      func         interface    selectcase         defer        go           map          structchan         else         goto         package      switchconst        fallthrough  if           range        typecontinue     for          import       return       var
*** 作符和分隔符

如下所示即golang中的 *** 作符,分隔符以及其他特殊符号

+    &     +=    &=     &&    ==    !=    (    )-    |     -=    |=     ||    <     <=    [    ]*    ^     *=    ^=     <-    >     >=    {    }/    <<    /=    <<=    ++    =     :=,;%    >>    %=    >>=    --    !     ...   .    :     &^          &^=
整形字面量

整形字面量是由一序列数字表示的整形常量,可由0或者0x作为其前缀,分别表示8进制和16进制数。

int_lit     = decimal_lit | octal_lit | hex_lit .decimal_lit = ( "1""9" ) { decimal_digit } .octal_lit   = "0" { octal_digit } .hex_lit     = "0" ( "x" | "X" ) hex_digit { hex_digit } . 42 0600 0xBadFace 170141183460469231731687303715884105727
Rune

Rune为Int32类型的别名,是一个以utf-8代码点(code point)表示的整形值。

字符串字面量

字符串字面量是一串字符序列表示的字符串常量。golang中有两种类型:原生字符串(foo bar)和解释后的utf-8形式的字符串(\u65e5\u672c\u8a9e)。

常量

golang中有布尔类型常量、rune常量、整形常量、浮点类型常量、字符串常量还有复数类型常量。其中,rune、整形、浮点型、复数型统称为数值型常量。

变量

变量是持有值的存储空间,其值类型由变量类型而决定。

var x interface{}  // x is nil and has static type interface{}var v *T           // v has value nil,static type *Tx = 42             // x has value 42 and dynamic type intx = v              // x has value (*T)(nil) and dynamic type *T
类型

类型决定值以及指定在该值上的运算 *** 作,类型可以显示指定,也可以不指定,而由编译器推断。

Type      = Typename | Typelit | "(" Type ")" .Typename  = IDentifIEr | QualifIEdIDent .Typelit   = ArrayType | StructType | PointerType | FunctionType | InterfaceType |        SliceType | MapType | ChannelType .

类型可以关联方法,一个任意的T类型的方法包含所有以T类型为接受者的方法,而指针类型*T的方法则包含以*T或者T为接受者的方法。

基础类型 布尔类型

golang中布尔类型为bool,值为truefalse,默认为false

数值类型

数值类型代表一组整形或者浮点类型值,在golang中预声明的类型有

uint8       the set of all unsigned  8-bit integers (0 to 255)uint16      the set of all unsigned 16-bit integers (0 to 65535)uint32      the set of all unsigned 32-bit integers (0 to 4294967295)uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)int8        the set of all signed  8-bit integers (-128 to 127)int16       the set of all signed 16-bit integers (-32768 to 32767)int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)float32     the set of all IEEE-754 32-bit floating-point numbersfloat64     the set of all IEEE-754 64-bit floating-point numberscomplex64   the set of all complex numbers with float32 real and imaginary partscomplex128  the set of all complex numbers with float64 real and imaginary partsbyte        alias for uint8rune        alias for int32

需要注意的是,这些类型的变量间不允许相互赋值或 *** 作,不然会在编译时引起错误。
另外,还有一些预声明的以特殊方式实现大小的类型

uint     either 32 or 64 bitsint      same size as uintuintptr  an unsigned integer large enough to store the uninterpreted bits of a pointer value

即使int的长度也是32位,但是int和int32仍然不相同,并不可以互用。

字符串类型

类型为string, 用内置的len 函数来计算其长度。

数组类型

数组是单一类型的元素序列,元素的个数成为数组的长度,定义方式如下

ArrayType   = "[" ArrayLength "]" ElementType .ArrayLength = Expression .ElementType = Type .

数组的长度也是数组类型的一部分,用内置函数len 来计算。

[32]byte[2*N] struct { x,y int32 }[1000]*float64[3][5]int[2][2][2]float64  // same as [2]([2]([2]float64))
Slice类型

slice是一种指向底层数组并能访问数组元素的描述形式,slice类型表示所有数组元素类型的slice集合,初始值为nil(空值)

SliceType = "[" "]" ElementType

与数组一样,用内置的len 来获取长度,但还可用内置的cap 函数来获取容量。
slice是一种引用类型,因此创建slice时,用make 函数来分配内存

make([]T,length)make([]T,length,capacity)
结构体类型

结构体是拥有字段或属性的集合,在结构体中可显示指定字段(标识符形式),也可以隐式指定(匿名字段),其中所有字段必须是唯一的。

StructType     = "struct" "{" { FIEldDecl ";" } "}" .FIEldDecl      = (IDentifIErList Type | AnonymousFIEld) [ Tag ] .AnonymousFIEld = [ "*" ] Typename .Tag            = string_lit .
// An empty struct.struct {}// A struct with 6 fIElds.struct {    x,y int    u float32    _ float32  // padding    A *[]int    F func()}

只声明类型而没有指定名称的字段叫做匿名字段,也称为嵌入字段或类型嵌入,嵌入类型须为T或者非接口型的*T类型,但T本身不能为指针类型。默认情况下,其非限定的类型名称则充当字段名。

// A struct with four anonymous fIElds of type T1,*T2,P.T3 and *P.T4struct {    T1        // fIEld name is T1    *T2       // fIEld name is T2    P.T3      // fIEld name is T3    *P.T4     // fIEld name is T4    x,y int  // fIEld names are x and y}

如下声明结构体的方式是非法的,因为字段名必须是唯一的结构体类型

struct {    T     // conflicts with anonymous fIEld *T and *P.T    *T    // conflicts with anonymous fIEld T and *P.T    *P.T  // conflicts with anonymous fIEld T and *T}

篇幅所限,其余类型见下文

http://www.jb51.cc/article/p-cohxflxd-ne.html

总结

以上是内存溢出为你收集整理的Golang学习笔记:语言规范之词汇元素和类型全部内容,希望文章能够帮你解决Golang学习笔记:语言规范之词汇元素和类型所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1286549.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存