Giter Site home page Giter Site logo

go-learning's Introduction

go-learning

##4 基本数据类型和数据结构

###4.1 文件名 关键字 标识符

  1. go文件名规则与java一致
  2. 25个关键字, func等,
  3. 36个预定义标识符,包含基本类型名称和基本函数
  4. 程序不以;结尾,除非多个语句写在同一行,则必须;认为区分,不鼓励

###4.2 基本结构和要素 ####4.2.1 包的概念 导入 可见性

  1. 包名都是小写字母,每个程序都包含main包
  2. 包的依赖关系决定了构建顺序,包是编译时的一个单元,依赖关系时,先从底层包开始编译
  3. 标准库包含大量的包,比如 fmt os
  4. import 关键字将一组包连接在一起
import "fmt" ;"os"
import(
"fmt"
"os"
)

import fm "fmt"
  1. 导入多个包时,顺序按照字母顺序排序,./包名 当前目录导入 /包名 绝对路径
  2. 可见性: 标识符(常量 变量 类型 函数名 结构字段)以大写字母开头则对包外可见,类似于public 小写字母开头,仅包内可见类似于private
  3. 不能import无用的包

4.2.2 函数

  1. 一个函数的格式,左大括号必须和函数名同一行,右大括号在函数体的下一行
func funcName (parmaList)(retList){
}
paramList :  a type1,b type2
retList : c type3,d type4

type IZ int
type(
IZ int
FZ float
STR string
)
  1. 一个可执行程序必须包含该main函数,init函数比main函数先执行

####4.2.3 注释

  1. 和java一样

4.2.4 类型

  1. 基本类型,int float bool string 结构化的 struct array slice channel map ,结构的话的没有真正的值,使用nil作为默认值,只描述类型行为的 interface
  2. 函数也可以作为一个返回类型
  3. 一个函数可以拥有多个返回值,逗号分隔
  4. 可以使用type定义自己的类型
  5. go是静态类型语言,必须在编译后确定类型

4.2.5 go程序的一般结构

go程序的执行顺序

  1. 导入main函数需要的所有包,然后在每个包内执行如下流程:
  2. 如果该包又导入了其他包则递归执行这两个步骤,每个包只会被倒入一次
  3. 如果包内有init函数,则导入后执行init函数,以导入包相反的顺序执行
  4. 执行完这一切候,开始执行main函数

4.2.6 类型转换

  1. go支持显示转换 valueA=typeA(valueB)
  2. 小精度转大精度没问题,大精度转小精度会精度丢失

4.2.7 go命名规范

  1. 名称不需要指出自己所属于的包

go-learning's People

Contributors

santiagogogo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.