Skip to content

Commit

Permalink
The code and doc for lesson 1-3
Browse files Browse the repository at this point in the history
  • Loading branch information
chaocai committed Mar 4, 2019
0 parents commit 0d7941b
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 0 deletions.
12 changes: 12 additions & 0 deletions code/ch1/hello/hello_world.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"fmt"
"os"
)

func main() {
if len(os.Args) > 1 {
fmt.Println("Hello World", os.Args[1])
}
}
24 changes: 24 additions & 0 deletions code/ch2/constant_test/constant_try_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package constant_test

import "testing"

const (
Monday = 1 + iota
Tuesday
Wednesday
)

const (
Readable = 1 << iota
Writable
Executable
)

func TestConstantTry(t *testing.T) {
t.Log(Monday, Tuesday)
}

func TestConstantTry1(t *testing.T) {
a := 1 //0001
t.Log(a&Readable == Readable, a&Writable == Writable, a&Executable == Executable)
}
35 changes: 35 additions & 0 deletions code/ch2/fib/fib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package fib

import (
"testing"
)

func TestFibList(t *testing.T) {
// var a int = 1
// var b int = 1
// var (
// a int = 1
// b = 1
// )
a := 1
// a := 1
b := 1
t.Log(a)
for i := 0; i < 5; i++ {
t.Log(" ", b)
tmp := a
a = b
b = tmp + a
}

}

func TestExchange(t *testing.T) {
a := 1
b := 2
// tmp := a
// a = b
// b = tmp
a, b = b, a
t.Log(a, b)
}
29 changes: 29 additions & 0 deletions code/ch3/type_test/type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package type_test

import "testing"

type MyInt int64

func TestImplicit(t *testing.T) {
var a int32 = 1
var b int64
b = int64(a)
var c MyInt
c = MyInt(b)
t.Log(a, b, c)
}

func TestPoint(t *testing.T) {
a := 1
aPtr := &a
//aPtr = aPtr + 1
t.Log(a, aPtr)
t.Logf("%T %T", a, aPtr)
}

func TestString(t *testing.T) {
var s string
t.Log("*" + s + "*") //初始化零值是“”
t.Log(len(s))

}
26 changes: 26 additions & 0 deletions code/ch4/operator_test/operator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package operator_test

import "testing"

const (
Readable = 1 << iota
Writable
Executable
)

func TestCompareArray(t *testing.T) {
a := [...]int{1, 2, 3, 4}
b := [...]int{1, 3, 2, 4}
// c := [...]int{1, 2, 3, 4, 5}
d := [...]int{1, 2, 3, 4}
t.Log(a == b)
//t.Log(a == c)
t.Log(a == d)
}

func TestBitClear(t *testing.T) {
a := 7 //0111
a = a &^ Readable
a = a &^ Executable
t.Log(a&Readable == Readable, a&Writable == Writable, a&Executable == Executable)
}
29 changes: 29 additions & 0 deletions code/ch5/condition/condition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package condition_test

import "testing"

func TestSwitchMultiCase(t *testing.T) {
for i := 0; i < 5; i++ {
switch i {
case 0, 2:
t.Log("Even")
case 1, 3:
t.Log("Odd")
default:
t.Log("it is not 0-3")
}
}
}

func TestSwitchCaseCondition(t *testing.T) {
for i := 0; i < 5; i++ {
switch {
case i%2 == 0:
t.Log("Even")
case i%2 == 1:
t.Log("Odd")
default:
t.Log("unknow")
}
}
}
11 changes: 11 additions & 0 deletions code/ch5/loop/loop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package loop_test

import "testing"

func TestWhileLoop(t *testing.T) {
n := 0
for n < 5 {
t.Log(n)
n++
}
}
28 changes: 28 additions & 0 deletions code/ch6/array_test/array_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package array_test

import "testing"

func TestArrayInit(t *testing.T) {
var arr [3]int
arr1 := [4]int{1, 2, 3, 4}
arr3 := [...]int{1, 3, 4, 5}
arr1[1] = 5
t.Log(arr[1], arr[2])
t.Log(arr1, arr3)
}

func TestArrayTravel(t *testing.T) {
arr3 := [...]int{1, 3, 4, 5}
for i := 0; i < len(arr3); i++ {
t.Log(arr3[i])
}
for _, e := range arr3 {
t.Log(e)
}
}

func TestArraySection(t *testing.T) {
arr3 := [...]int{1, 2, 3, 4, 5}
arr3_sec := arr3[:]
t.Log(arr3_sec)
}
49 changes: 49 additions & 0 deletions code/ch6/slice_test/slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package slice_test

import "testing"

func TestSliceInit(t *testing.T) {
var s0 []int
t.Log(len(s0), cap(s0))
s0 = append(s0, 1)
t.Log(len(s0), cap(s0))

s1 := []int{1, 2, 3, 4}
t.Log(len(s1), cap(s1))

s2 := make([]int, 3, 5)
t.Log(len(s2), cap(s2))
t.Log(s2[0], s2[1], s2[2])
s2 = append(s2, 1)
t.Log(s2[0], s2[1], s2[2], s2[3])
t.Log(len(s2), cap(s2))
}

func TestSliceGrowing(t *testing.T) {
s := []int{}
for i := 0; i < 10; i++ {
s = append(s, i)
t.Log(len(s), cap(s))
}
}

func TestSliceShareMemory(t *testing.T) {
year := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"}
Q2 := year[3:6]
t.Log(Q2, len(Q2), cap(Q2))
summer := year[5:8]
t.Log(summer, len(summer), cap(summer))
summer[0] = "Unknow"
t.Log(Q2)
t.Log(year)
}

func TestSliceComparing(t *testing.T) {
a := []int{1, 2, 3, 4}
b := []int{1, 2, 3, 4}
// if a == b { //切片只能和nil比较
// t.Log("equal")
// }
t.Log(a, b)
}
Binary file added doc/Go语言从入门到实战1-2章课件.pdf
Binary file not shown.

0 comments on commit 0d7941b

Please sign in to comment.