前端analysis | 3w & 1h

go学习笔记一

2022-07-26

go

语言特点

  • 全新的静态类型开发语言
  • 具有自动垃圾回收、丰富的内置类型
  • 函数多返回值、错误处理
  • 匿名函数、闭包、并发编程
  • 反射、defer、接口
  • 简洁、更加安全、开源等特性
  • 充分利用Cpu多核,运行速度媲美C、C++
  • 内置运行时,支持基础、对象等,开发工具丰富
  • 标准库完备,强大网络库,web服务开发容易,尤其适合服务器编程、网络编程、分布式编程,特别适合云计算领域
  • 提供了海量并行的支持,适合游戏服务端的开发
  • 另外,编译运行快 + 学习上手快,学习曲线并不陡峭

基础

go 支持的命令

  • bug : start a bug report
  • build : compile packages and dependencies
  • clean : remove object files and cached files
  • doc : show documentation for package or symbol
  • env : print Go environment information
  • fix : update packages to use new APIs
  • fmt : gofmt (reformat) package sources
  • generate : generate Go files by processing source
  • get : add dependencies to current module and install them
  • install : compile and install packages and dependencies
  • list : list packages or modules
  • mod : module maintenance
  • run : compile and run Go program
  • test : test packages
  • tool : run specified go tool
  • version : print Go version
  • vet : report likely mistakes in packages

一个go文件组成

  • 包声明

    开头定义包名,形成命名空间; 与文件名没有任何关系,两者可以不一致,也可以一致;

  • 引入包
  • 函数
  • 变量
  • 语句 & 表达式
  • 注释

go vs js

特点 go js
书写 不需要;结尾 不需要;结尾
编译器 先编译在执行 解释性语言
标准 仅此一套 commonjs、amd、umd、es module
注释 支持 //, /** / 支持 //, /**/
字符串拼接 “a” + “b” “a” + “b” or ${a}
变量声明 var age int (直接跟在后面,不用 var age: int ) var age or let age or const age
数据类型 数据: int,float32, float64,支持complex64,complex128,还有其他无符号整数 number

go 默认值

  • 数值类型(包括complex64/128)为 0
  • 布尔类型为 false
  • 字符串为 “”(空字符串)

以下几种类型为 nil:
var a *int
var a []int
var a map[string] int
var a chan int
var a func(string) int
var a error // error 是接口

使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏