To Be A Gopher
To Be A Gopher

To Be A Gopher

Hello World

项目结构
notion image
package main // 名称不一定要和目录结构一致, 避免陷入Java误区 import "fmt" // 导入依赖, 打印的包 func main() { fmt.Println("hello world") // 控制台输出 }

获取命令行参数, main方法返回程序执行状态

package main // 和java不同的是, package和目录不一定是完全相同的. import ( "fmt" "os" ) func main() { // 获取命令行参数 if len(os.Args) > 1 { fmt.Println("hello world", os.Args[1]) } fmt.Println("hello world") os.Exit(0) // 返回程序执行状态的方式 }

单元测试

编写规范

  1. 源文件必须以_test.go结尾.
  1. 测试方法模版必须满足: TestXXXX(t *testing.T)形式
示例
package try_test import "testing" func TestFirstTry(t *testing.T) { t.Log("My first try") }

斐波那契数列输出示例

package fib import ( "fmt" "testing" ) func TestFibList(t *testing.T) { //var a int = 1 //var b int = 1 a := 1 b := 1 fmt.Print(a, " ") for i := 0; i < 5; i++ { fmt.Print(" ", b) tm := a a = b b = tm + a } fmt.Println() }

交换两个变量的值

func TestExchange(t *testing.T) { a := 1 b := 2 //temp := a //a = b //b = temp a, b = b, a // 交换两个变量的值. t.Log(a, b) }

常量定义

package constant_test import ( "testing" ) const ( // 连续常量的定义方式 Monday = iota + 1 Tuesday Wednesday ) const ( Readable = 1 << iota Writable Executable )

数据类型转换

package type_test import "testing" type MyInt int64 func TestImplicint(t *testing.T) { var a int32 = 42 var b int64 //b=a // 不支持隐示类型装换 b = int64(a) // 显示类型转换 var c MyInt //c = b // 同类型别名之间也无法直接进行隐式转换 c = MyInt(b) // 显示转换 t.Log(a, b, c) } func TestPoint(t *testing.T) { a := 1 aPtr := &a //aPtr=aPtr+1 // 指针不支持运算 t.Log(aPtr) t.Logf("%T %T", a, aPtr) } func TestString(t *testing.T) { var s string // default "" 空字符串 t.Log("#" + s + "*") t.Log(len(s)) }

操作运算符

package operator_test import "testing" const ( Readable = 1 << iota Writable Executable ) func TestOperatorArray(t *testing.T) { a := [...]int{1, 2, 3} d := [...]int{1, 2, 3} b := [...]int{1, 3, 2} c := [...]int{3, 2, 1} //e := [...]int{1, 2, 5, 6} t.Log(a == b) t.Log(a == c) t.Log(a == d) //t.Log(a == e) // 长度不同的数组直接比较会报错 } func TestBitClear(t *testing.T) { a := 7 // 0111 a = a &^ Readable t.Log(a&Readable == Readable, a&Writable == Writable, a&Executable == Executable) }

循环

package loop import "testing" func TestLoop(t *testing.T) { n := 0 for n < 5 { t.Log(n) n++ } }

分支条件

package condition import "testing" func TestIfSec(t *testing.T) { if a := 1 == 1; a { t.Log("1==1") } else { t.Log("1!=1") } } func TestSwitch(t *testing.T) { for i := 0; i < 10; i++ { switch i { case 0, 1, 2: t.Log("aaaaa") case 3, 4, 5: t.Log("bbbbb") default: t.Log("ccccc") } } }

数组

package array_test import "testing" func TestArray(t *testing.T) { var arr [3]int //arr1 := [4]int{1, 2, 3, 4} //arr2 := [...]int{1, 2, 3, 4, 5} t.Log(arr[0], arr[1], arr[2]) } func TestIteratorArray(t *testing.T) { arr := [...]int{11, 12, 13, 14, 15} for i := range arr { t.Log(arr[i]) } for i, e := range arr { t.Log(i, e) } } func TestArraySlice(t *testing.T) { // 规则, 含头部含尾, 不支持负数 arr := [...]int{11, 12, 13, 14, 15} arr1 := arr[1:2] arr2 := arr[:3] arr3 := arr[3:] arr4 := arr[:] t.Log(arr1, arr2, arr3, arr4) }

切片

package slice_test import "testing" func TestSliceInit(t *testing.T) { var s1 []int t.Log(len(s1), cap(s1)) s1 = append(s1, 100) t.Log(len(s1), cap(s1)) var s2 = []int{10, 20, 30} t.Log(len(s2), cap(s2)) var s3 = make([]int, 3, 5) t.Log(len(s3), cap(s3)) t.Log(s3[0], s3[1], s3[2]) s3 = append(s3, 22) t.Log(s3[0], s3[1], s3[2], s3[3]) t.Log(s3[0], s3[1], s3[2], s3[3], s3[4]) } func TestSliceTravel(t *testing.T) { var s1 []int for i := 0; i < 10; i++ { s1 = append(s1, i) t.Log(len(s1), cap(s1)) } }