Go
GO语言基础
安装&使用
安装
下载源码包到你想放置的目录,然后设置环境变量
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/data/work
export PATH=$PATH:$GOPATH/bin
使用
有2种方式运行程序
解释性语言的常用运行方式
go run main.go
先编译成二进制,然后运行
go build -o main main.go & ./main
跨平台编译, 编译完后把二进制包以及必要的资源文件copy到需要使用的机器上,无需安装环境即可使用
GOOS=darwin GOARCH=amd64 go build -o main main.go
GOOS=windows GOARCH=amd64 go build -o main main.go
GOOS=linux GOARCH=amd64 go build -o main main.go
语言基础
go语言组成
go语言一共有25个关键字,基本上把这25个关键字“掌握”就能写go语言程序了
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
go语言内置的数据类型标识符(20个)
数值(16)
* 整型(12)
byte int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
* 浮点型(2)
float32 float64
* 复数型(2)
complex64 complex128
字符和字符串型(2)
string rune
接口型(1)
error
布尔型(1)
bool
go语言内置函数(15个)
make new len cap append copy delete panic recover close complex real image print println
常量标识符(4个)
true false
iota
nil
空白标识符(1个)
_ 一般用在引入包和变量返回时使用,只是引入包的时候以及忽略变量返回值的时候使用
go语言的操作符(47个)
+ & += &= && == != ( )
- | -= |= || < <= [ ]
* ^ *= ^= <- > >= { }
/ << /= <<= ++ = := , ;
% >> %= >>= -- ! ... . :
&^ &^=
一个go语言程序的组成
package main
import "fmt"
func main() {
fmt.Println("hello world!!!")
}
关键字
package import func
标识符
main fmt Println
字面量
"fmt" "hello world!!"
操作符
( ) { } .
送给所有学习go的小伙伴的几句话
Don't think you have to know everything to be useful
Even senior Go Developers don't know it all
Just get stuck in, we'll help if we can
Don't obsess over performance of code, obsess over making it clear
Have fun