forked from geektime-geekbang/go_learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chaocai
committed
Mar 4, 2019
0 parents
commit 0d7941b
Showing
10 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.