这篇文章主要介绍Go语言中的数据类型和构成要素,包括标识符、关键字、运算符、标点符号等。

Hello,world!

和其他语言一样,开始先来个Hello,world 程序:

// 包声明
package main

// 依赖包导入
import "fmt"

// 主函数
func main() {
    // 打印 Hello World !
    fmt.Println("Hello World !")
}

数据类型

基本类型

  • 布尔类型,bool,其值为:true或者false

  • 数值类型

    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 numbers
    float64     the set of all IEEE-754 64-bit floating-point numbers
    complex64   the set of all complex numbers with float32 real and imaginary parts
    complex128  the set of all complex numbers with float64 real and imaginary parts
    byte        alias for uint8
    rune        alias for int32
  • 字符串类型,string

高级类型

  • 数组

  • 切片

  • 字典(Map)

  • 函数(Function)

  • 接口(Interface)

  • 结构体(Struct)

标识符

标识符,从字面意思来说,就是用来标识变量、类型等程序实体的符号。标识符只能由字母、下划线(_)和数字组成,且第一个字符必须为字母或下划线。

有一些标识符是程序源码中预定义的标识符:

  • 基本数据类型

    • bool byte complex64 complex128 error float32 float64
    • int int8 int16 int32 int64 rune string
    • uint uint8 uint16 uint32 uint64 uintptr
  • 常量 true false iota

  • 空值 nil

  • 内置函数 append cap close complex copy delete imag len make new panic print println real recover

关键字

关键字是编程语言保留的字符序列,不能用作标识符:

break        default      func         interface    select
case         defer        go           map          struct
chan         else         goto         package      switch
const        fallthrough  if           range        type
continue     for          import       return       var

运算符和标点符号

运算符包括赋值运算符:

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