@[toc]
分享一下学习go的网站:
国外的网站:https://golang.org
go编程网站:https://golang.google.cn/ (go的包,函数等均可以在这里查看)
菜鸟教程-Go :菜鸟
易百教程-Go:易百教程
W3c-Go:w3c
极客-Go:极客
Go高级编程
GO语言中文库:https://studygolang.com/pkgdoc(常用)
1.GO语言的注释
//单行注释/*多行注释*/
2.GO语言的转义字符
\t :对齐功能\n:换行符\“:一个”字符\r:回车
代码:
package mainimport "fmt"func main() {fmt.Println("hello\tdadaw")fmt.Println("hello\ndadaw")fmt.Println("hello\rdadaw")fmt.Println("hello\"dadaw")}
效果:
3.GO语言的基本类型
3.1数字类型
数据类型工作中就是把数据分成内存大小不同的数据,充分利用
- uint8 位整型 (0 到 255)
- uint16 16位整型 (0 到 65535)
- uint32 32位整型 (0 到 4294967295)
- uint64 64位整型 (0 到 18446744073709551615)
- int8 8位整型 (-128 到 127)
- int16 16位整型 (-32768 到 32767)
- int32 32位整型 (-2147483648 到 2147483647)
- int64 64 位整型 (-9223372036854775808 到 9223372036854775807)
- uintptr 无符号整型,用于存放一个指针
3.2浮点型
float32 32位浮点型数
float64 64位浮点型数
complex64 32位实数和虚数
complex128 64位实数和虚数
3.3布尔型
(a)true (b)false
3.3字符串型
预先声明是string
3.4派生类型
(a)指针类型
(b)数组类型
(c)结构类型
(d)联合类型
(e)函数类型
(f)切片类型
(g)函数类型
(h)接口类型
(i) 类型
4.GO语言变量
go是静态类型语言,因此声明的变量必须是明确类型的,一般用var关键字
变量=变量名+变量值+类型
变量规范:变量由字母,数字,下划线组成,不能以数字开头
-标准声明变量的格式:
var 变量名 变量类型var 变量名,变量名 *变量类型
代码:
var num intvar num,num2 *int
-批量声明格式:
var (a intb stringc []float32d func() boole struct {x int})
简短格式:(声明了数据类型的变量是不能使用简短格式输出的)
- 不能提供数据类型
- 只能在函数内部使用
- 定义的变量,显示初始化
名字:= 表达式
代码:
func main() {a:=100c,d:=50,"huawei"}
常用声明赋值总结
package mainimport "fmt"//全局声明的变量类型var (name string //字符串age int //整数型salary float32 //浮点型)func main() {//以下均是局部变量// 简短式表达式a, b, c := 100, 100.12345, "你好"fmt.Println(a)fmt.Println(b)fmt.Println(c)//声明多个变量类型表达式()var d, e, f = 18.5, "小王", 4500fmt.Println(d, e, f)//这里使用全局变量声明的类型,拿来使用输出name = "huawei"age = 18salary = 1000fmt.Println(name)fmt.Println(age)fmt.Println(salary)}输出:100100.12345你好18.5 小王 4500huawei181000
5.GO语言常量
常量(const)一般被定义后,在程序运行时,是不会被修改的量,iota除外
常量定义的格式:(变量类型可以省略,在定义值时,编译器可以自动推断类型)
const defintion [type] = values
多个相同类型的常量定义:
const defintion_1,defintion_2 = value01,value02
变量的案例:
package mainimport "fmt"//定义的变量一般在全局范围const language string = "GO"const scholar = "caicai"func main() {//局部变量内使用变量fmt.Println("this is const")fmt.Println("语言:", language, "学者:", scholar)}运行输出:this is const语言: GO 学者: caicai
特殊常量iota
iota 简单记:i/o它, 是一个可以改变的常量,每遇到一个const关键字时会被重置为0,在const内的数字会被自动增加1,直接比喻为常量计数器
iota的案例:
package mainimport "fmt"func main() {fmt.Println("this is iota")const (a = iota //a=iota开始计数,a=0b //1c //2d //3e //4)fmt.Println(a, b, c, d, e)const (aa = iota //aa=iota开始计数,aa=0bb = "linux" //bb赋予单独值,iota此时计数为1(不显示计数数字,只显示赋予的值)cc = "java" //cc赋予独值,iota此时计数为2(不显示计数数字,只显示赋予的值)dd = "python" //dd赋予独值,iota此时计数为3(不显示计数数字,只显示赋予的值)ee = iota //ee没有单独赋予值,恢复计数为4(显示计数数字))fmt.Println(aa, bb, cc, dd, ee)const (aaa = iotabbb = "三国演义"ccc = 500ddd = 600 //ddd赋予单独值,iota此时计数为3(不显示计数数字,只显示赋予的值)eee //eee继承ddd的赋予值,iota此时计数为4(不显示计数数字,只显示赋予的值)fff //fff继承eee的赋予值,iota此时计数为5(不显示计数数字,只显示赋予的值)ggg = iota //恢复计数,ggg=6)fmt.Println(aaa, bbb, ccc, ddd, eee, fff, ggg)}运行输出:this is iota0 1 2 3 40 linux java python 40 三国演义 500 600 600 600 6
kubernetes中对于iota的使用
针对于pod组件的Success、Error、Unscheduleable、UnschedulableAndUnresolvable、Wait、Skip这六种状态字段表达的效果为Success=0、Error=1等
package mainimport "fmt"func main() {type Code int //定义一个Code的整数型const (// Success means that plugin ran correctly and found pod schedulable.// NOTE: A nil status is also considered as "Success".Success Code=iota //开始计数,Success=0// Error is used for internal plugin errors, unexpected input, etc.Error //iota自增加1,Error=1// Unschedulable is used when a plugin finds a pod unschedulable. The scheduler might attempt to// preempt other pods to get this pod scheduled. Use UnschedulableAndUnresolvable to make the// scheduler skip preemption.// The accompanying status message should explain why the pod is unschedulable.Unschedulable //iota自增加1,Unschedulable=2// UnschedulableAndUnresolvable is used when a PreFilter plugin finds a pod unschedulable and// preemption would not change anything. Plugins should return Unschedulable if it is possible// that the pod can get scheduled with preemption.// The accompanying status message should explain why the pod is unschedulable.UnschedulableAndUnresolvable //iota自增加1,UnschedulableAndUnresolvable=3// Wait is used when a Permit plugin finds a pod scheduling should wait.Wait //iota自增加1,Wait=4// Skip is used when a Bind plugin chooses to skip binding.Skip //iota自增加1,Wait=5)fmt.Println(Success, Error, Unschedulable, UnschedulableAndUnresolvable, Wait, Skip)}输出显示:[Running] go run "/root/workspace/goproject/src/go_code/project01/main/kubernet-iota.go"0 1 2 3 4 5
6.GO语言运算符
6.1GO运算的分类
算术运算符
关系运算符
逻辑运算符
位运算符
赋值运算符
其他运算符
6.2算术运算
运算符 描述 表达式+ 相加 A + B- 相减 A - B* 相乘 A * B/ 相除 A/ B% 求余 A % B++ 自增 A++-- 自减 A--
实例
package mainimport "fmt"func main() {a := 20b := 10var c intc = a + bfmt.Println(c)c = a * bfmt.Println(c)c = a / bfmt.Println(c)c = a % bfmt.Println(c)a++fmt.Println(a)b++fmt.Println(b)}运行输出:[Running] go run "/root/workspace/goproject/src/go_code/project01/main/arich.go"30200202111
6.3关系运算
== 检查两个值是否相等,如果相等返回 True 否则返回 False。 (A == B) 为 False!= 检查两个值是否不相等,如果不相等返回 True 否则返回 False。 (A != B) 为 True> 检查左边值是否大于右边值,如果是返回 True 否则返回 False。 (A > B) 为 False< 检查左边值是否小于右边值,如果是返回 True 否则返回 False。 (A < B) 为 True>= 检查左边值是否大于等于右边值,如果是返回 True 否则返回 False。 (A >= B) 为 False<= 检查左边值是否小于等于右边值,如果是返回 True 否则返回 False。 (A <= B) 为 True
6.4逻辑运算
&& 逻辑 AND 运算符 如果两边的操作数都是 True,则条件 True,否则为 False|| 逻辑 OR 运算符 如果两边的操作数有一个 True,则条件 True,否则为 False! 逻辑 NOT 运算符 如果条件为 True,则逻辑 NOT 条件 False,否则为 True
6.5赋值运算
= 表达式赋值,给予左边+= 相加再赋值-= 相减再赋值*= 相乘后再赋值/= 相除后再赋值%= 求余后再赋值<<= 左移后赋值>>= 右移后赋值&= 按位与后赋值^= 按位异或后赋值|= 按位或后赋值
6.6位运算
a b a&b a|b a^b0 0 0 0 00 1 0 1 11 1 1 1 01 0 0 1 1
6.7其它运算
& 返回变量存储地址* 指针变量
7go条件语句
if 语句
if else语句
if else else if嵌套语句
switch语句
7.1if语句
if 布尔表达式 {/* 当布尔表达式为 true 时执行 */}
7.2if else语句
d := 400e := 500if d >= e {fmt.Println("d>=e")} else {fmt.Println("d<e")}运行输出:d<e
7.3if嵌套语句
if 布尔表达式noe {/* 在布尔表达式 one 为 true 时执行 */if 布尔表达式 two {/* 在布尔表达式two 为 true 时执行 */}}
7.4switch语句
switch 语句用于基于不同条件执行不同动作,每一个 case 分支都是唯一的,从上至下逐一测试,直到匹配为止。
switch 语句执行的过程从上至下,直到找到匹配项,匹配项后面也不需要再加 break。
switch 默认情况下 case 最后自带 break 语句,匹配成功后就不会执行其他 case,如果我们需要执行后面的 case,可以使用 fallthrough
package mainimport "fmt"func main() {var grade string = "A"var marks int = 90switch marks {case 90:grade = "A"case 80:grade = "B"case 70:grade = "C"case 60:grade = "D"default:grade = "E"}switch {case grade == "A":fmt.Println("优秀\n")case grade == "B":fmt.Println("良\n")case grade == "C", grade == "D":fmt.Println("一般\n")case grade == "E":fmt.Println("很差\n")}}运行输出:优秀
8.go循环语句
8.1for循环语句格式:
for 变量初始条件; 循环条件; 变量迭代{}for 循环条件{}for {}
输出5遍:2月的天,3月的地
package mainimport "fmt"func main() {for init := 1; init <= 5; init++ {fmt.Println("2月的天,3月的地")}}运行输出:[Running] go run "/root/workspace/goproject/src/go_code/project01/main/for.go"2月的天,3月的地2月的天,3月的地2月的天,3月的地2月的天,3月的地2月的天,3月的地
输出30-60数字
package mainimport "fmt"func main() {for i := 30; i <= 60; i++ {fmt.Println(i)}}运行输出:[Running] go run "/root/workspace/goproject/src/go_code/project01/main/for.go"30313233343536373839404142434445464748495051525354555657585960
8.2循环控制语句
break:中断for循环,跳出switch语句continue:跳出当前循环,继续下一轮循环goto:可以无条件地转移到过程中指定的行
break有条件的中止循环体的输出
输出30-60数字,但是不要50-60
package mainimport "fmt"func main() {for i := 30; i <= 60; i++ {fmt.Println(i)if i > 50 {break //循环到i=50时,直接跳出循环}}}运行循环:[Running] go run "/root/workspace/goproject/src/go_code/project01/main/for.go"303132333435363738394041424344454647484950
打印九九乘法表
%d:输出宽度也是输出十进制整数 printf:格式输出函数
package mainimport "fmt"func main() {//处理九行for y := 1; y <= 9; y++ {// 针对于y的行处理列for x := 1; x <= y; x++ {fmt.Printf("%d*%d=%d ", x, y, x*y)}// 手动生成回车fmt.Println()}}运行输出:1*1=11*2=2 2*2=41*3=3 2*3=6 3*3=91*4=4 2*4=8 3*4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5*5=251*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=491*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=641*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
