##4 基本数据类型和数据结构
###4.1 文件名 关键字 标识符
- go文件名规则与java一致
- 25个关键字, func等,
- 36个预定义标识符,包含基本类型名称和基本函数
- 程序不以;结尾,除非多个语句写在同一行,则必须;认为区分,不鼓励
###4.2 基本结构和要素 ####4.2.1 包的概念 导入 可见性
- 包名都是小写字母,每个程序都包含main包
- 包的依赖关系决定了构建顺序,包是编译时的一个单元,依赖关系时,先从底层包开始编译
- 标准库包含大量的包,比如 fmt os
- import 关键字将一组包连接在一起
import "fmt" ;"os"
import(
"fmt"
"os"
)
import fm "fmt"
- 导入多个包时,顺序按照字母顺序排序,./包名 当前目录导入 /包名 绝对路径
- 可见性: 标识符(常量 变量 类型 函数名 结构字段)以大写字母开头则对包外可见,类似于public 小写字母开头,仅包内可见类似于private
- 不能import无用的包
- 一个函数的格式,左大括号必须和函数名同一行,右大括号在函数体的下一行
func funcName (parmaList)(retList){
}
paramList : a type1,b type2
retList : c type3,d type4
type IZ int
type(
IZ int
FZ float
STR string
)
- 一个可执行程序必须包含该main函数,init函数比main函数先执行
####4.2.3 注释
- 和java一样
- 基本类型,int float bool string 结构化的 struct array slice channel map ,结构的话的没有真正的值,使用nil作为默认值,只描述类型行为的 interface
- 函数也可以作为一个返回类型
- 一个函数可以拥有多个返回值,逗号分隔
- 可以使用type定义自己的类型
- go是静态类型语言,必须在编译后确定类型
go程序的执行顺序
- 导入main函数需要的所有包,然后在每个包内执行如下流程:
- 如果该包又导入了其他包则递归执行这两个步骤,每个包只会被倒入一次
- 如果包内有init函数,则导入后执行init函数,以导入包相反的顺序执行
- 执行完这一切候,开始执行main函数
- go支持显示转换 valueA=typeA(valueB)
- 小精度转大精度没问题,大精度转小精度会精度丢失
- 名称不需要指出自己所属于的包