艺多不压身,学习一下最近蛮火的Go语言,整理一下笔记。相关Code和笔记也放到了Git上,传送门。linux
1.从Hello world开始git
2.测试程序,变量,常量github
常量定义方面windows
// 1.快速设置连续值 const ( Monday = iota + 1 Tuseday Wednesday ... Sunday ) // 2. 表状态的bite位也能够这么玩 Const ( Open = 1 << iota Close Pending )
3.基本数据类型数组
bool
string
int int8 int16 int32 int64
uint unit8 unit16 unit32 unit64 uintptr
byte // alias for unit8
rune // alias for int32, represents a Unicode code point !!!这个类型之后会详详细介绍,暂且放一放
float32 float64
complex64 complex128函数
类型的预约义值
例如:
math.MaxInt64
math.MaxFloat64
math.MaxUnit32学习
指针类型 于其余主要变成语言的差别
1. 不支持指针运算
2. string是值类型,其默认的初始化值为空字符串,而不是nil测试
4.运算符ui
算术运算符spa
比较运算符
经常使用运算符与别的语言没什么差别,但什么对象能够比较稍有差别,往后补充
例:用==比较数组
相同维数且含有相同个数元素的数组才可比较
每一个元素都相等才相等
逻辑运算符
与别的语言没由什么差别
位运算符
& | ^ << >>没什么差别
与其余语言的差别 &^ 按位置零 (按位清零) 运算符
1 &^ 0 -- 1
1 &^ 1 -- 0
0 &^ 1 -- 0
0 &^ 0 -- 0
该运算符这样计算, 只要操做符右边的运算数上的位置为1,不管左边对应位置上的运算数是多少都清零。
右边操做符上的操做数为0时则左边原来是什么就显示什么。
注意,使用Go语言就要使用Go的特色,写出真正的Go程序而不是将其余语言翻译成Go.
5.条件和循环
循环
Go语言仅支持循环关键字for
例 for j:= 7; j <= 9; j++ (不须要括号括起来)
while条件循环 while (n < 5) Go版本 n := 0 for n < 5 { n++ fmt.Println(n) } 无限循环 while(true) Go版本 n := 0 for { ... }
if条件
例: if condition { ... } else { ... } if condition - 1 { ... } else if condition - 2 { ... } else { ... }
区别:
if vardeclaration; condition { // code to be executed if condition is true }
switch条件与其余语言的差别
写法举例: 1. switch os := runtime.GOOS; os { case "darwin": fmt.Println("OS X.") //break case "linux": fmt.Println("Linux.") default: // freebsd, openbsd, // plan9, windows... fmt.Printf("%s.", os) } 2. switch { case 0 <= Num && Num <= 3: fmt.Prinft("0-3") case 4 <= Num && Num <= 6: fmt.Prinft("4-6") case 7 <= Num && Num <= 9: fmt.Prinft("7-9") }