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 18, 2019
1 parent
54becad
commit 946fa8f
Showing
16 changed files
with
528 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,28 @@ | ||
package customer_type | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type IntConv func(op int) int | ||
|
||
func timeSpent(inner IntConv) IntConv { | ||
return func(n int) int { | ||
start := time.Now() | ||
ret := inner(n) | ||
fmt.Println("time spent:", time.Since(start).Seconds()) | ||
return ret | ||
} | ||
} | ||
|
||
func slowFun(op int) int { | ||
time.Sleep(time.Second * 1) | ||
return op | ||
} | ||
|
||
func TestFn(t *testing.T) { | ||
tsSF := timeSpent(slowFun) | ||
t.Log(tsSF(10)) | ||
} |
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,44 @@ | ||
package encap | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"unsafe" | ||
) | ||
|
||
type Employee struct { | ||
Id string | ||
Name string | ||
Age int | ||
} | ||
|
||
// func (e *Employee) String() string { | ||
// fmt.Printf("Address is %x", unsafe.Pointer(&e.Name)) | ||
// return fmt.Sprintf("ID:%s/Name:%s/Age:%d", e.Id, e.Name, e.Age) | ||
// } | ||
|
||
func (e Employee) String() string { | ||
fmt.Printf("Address is %x\n", unsafe.Pointer(&e.Name)) | ||
return fmt.Sprintf("ID:%s-Name:%s-Age:%d", e.Id, e.Name, e.Age) | ||
} | ||
|
||
func TestCreateEmployeeObj(t *testing.T) { | ||
e := Employee{"0", "Bob", 20} | ||
e1 := Employee{Name: "Mike", Age: 30} | ||
e2 := new(Employee) //返回指针 | ||
e2.Id = "2" | ||
e2.Age = 22 | ||
e2.Name = "Rose" | ||
t.Log(e) | ||
t.Log(e1) | ||
t.Log(e1.Id) | ||
t.Log(e2) | ||
t.Logf("e is %T", e) | ||
t.Logf("e2 is %T", e2) | ||
} | ||
|
||
func TestStructOperations(t *testing.T) { | ||
e := Employee{"0", "Bob", 20} | ||
fmt.Printf("Address is %x\n", unsafe.Pointer(&e.Name)) | ||
t.Log(e.String()) | ||
} |
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,20 @@ | ||
package interface_test | ||
|
||
import "testing" | ||
|
||
type Programmer interface { | ||
WriteHelloWorld() string | ||
} | ||
|
||
type GoProgrammer struct { | ||
} | ||
|
||
func (g *GoProgrammer) WriteHelloWorld() string { | ||
return "fmt.Println(\"Hello World\")" | ||
} | ||
|
||
func TestClient(t *testing.T) { | ||
var p Programmer | ||
p = new(GoProgrammer) | ||
t.Log(p.WriteHelloWorld()) | ||
} |
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,32 @@ | ||
package extension | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
type Pet struct { | ||
} | ||
|
||
func (p *Pet) Speak() { | ||
fmt.Print("...") | ||
} | ||
|
||
func (p *Pet) SpeakTo(host string) { | ||
p.Speak() | ||
fmt.Println(" ", host) | ||
} | ||
|
||
type Dog struct { | ||
Pet | ||
} | ||
|
||
func (d *Dog) Speak() { | ||
fmt.Print("Wang!") | ||
} | ||
|
||
func TestDog(t *testing.T) { | ||
dog := new(Dog) | ||
|
||
dog.SpeakTo("Chao") | ||
} |
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,31 @@ | ||
package empty_interface | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func DoSomething(p interface{}) { | ||
// if i, ok := p.(int); ok { | ||
// fmt.Println("Integer", i) | ||
// return | ||
// } | ||
// if s, ok := p.(string); ok { | ||
// fmt.Println("stirng", s) | ||
// return | ||
// } | ||
// fmt.Println("Unknow Type") | ||
switch v := p.(type) { | ||
case int: | ||
fmt.Println("Integer", v) | ||
case string: | ||
fmt.Println("String", v) | ||
default: | ||
fmt.Println("Unknow Type") | ||
} | ||
} | ||
|
||
func TestEmptyInterfaceAssertion(t *testing.T) { | ||
DoSomething(10) | ||
DoSomething("10") | ||
} |
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,36 @@ | ||
package polymorphism | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
type Code string | ||
type Programmer interface { | ||
WriteHelloWorld() Code | ||
} | ||
|
||
type GoProgrammer struct { | ||
} | ||
|
||
func (p *GoProgrammer) WriteHelloWorld() Code { | ||
return "fmt.Println(\"Hello World!\")" | ||
} | ||
|
||
type JavaProgrammer struct { | ||
} | ||
|
||
func (p *JavaProgrammer) WriteHelloWorld() Code { | ||
return "System.out.Println(\"Hello World!\")" | ||
} | ||
|
||
func writeFirstProgram(p Programmer) { | ||
fmt.Printf("%T %v\n", p, p.WriteHelloWorld()) | ||
} | ||
|
||
func TestPolymorphism(t *testing.T) { | ||
goProg := &GoProgrammer{} | ||
javaProg := new(JavaProgrammer) | ||
writeFirstProgram(goProg) | ||
writeFirstProgram(javaProg) | ||
} |
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,74 @@ | ||
package err_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
var LessThanTwoError = errors.New("n should be not less than 2") | ||
var LargerThenHundredError = errors.New("n should be not larger than 100") | ||
|
||
func GetFibonacci(n int) ([]int, error) { | ||
if n < 2 { | ||
return nil, LessThanTwoError | ||
} | ||
if n > 100 { | ||
return nil, LargerThenHundredError | ||
} | ||
fibList := []int{1, 1} | ||
|
||
for i := 2; /*短变量声明 := */ i < n; i++ { | ||
fibList = append(fibList, fibList[i-2]+fibList[i-1]) | ||
} | ||
return fibList, nil | ||
} | ||
|
||
func TestGetFibonacci(t *testing.T) { | ||
if v, err := GetFibonacci(1); err != nil { | ||
if err == LessThanTwoError { | ||
fmt.Println("It is less.") | ||
} | ||
t.Error(err) | ||
} else { | ||
t.Log(v) | ||
} | ||
|
||
} | ||
|
||
func GetFibonacci1(str string) { | ||
var ( | ||
i int | ||
err error | ||
list []int | ||
) | ||
if i, err = strconv.Atoi(str); err == nil { | ||
if list, err = GetFibonacci(i); err == nil { | ||
fmt.Println(list) | ||
} else { | ||
fmt.Println("Error", err) | ||
} | ||
} else { | ||
fmt.Println("Error", err) | ||
} | ||
} | ||
|
||
func GetFibonacci2(str string) { | ||
var ( | ||
i int | ||
err error | ||
list []int | ||
) | ||
if i, err = strconv.Atoi(str); err != nil { | ||
fmt.Println("Error", err) | ||
return | ||
} | ||
if list, err = GetFibonacci(i); err != nil { | ||
|
||
fmt.Println("Error", err) | ||
return | ||
} | ||
fmt.Println(list) | ||
|
||
} |
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,20 @@ | ||
package panic_recover | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestPanicVxExit(t *testing.T) { | ||
|
||
defer func() { | ||
if err := recover(); err != nil { | ||
fmt.Println("recovered from ", err) | ||
} | ||
}() | ||
fmt.Println("Start") | ||
panic(errors.New("Something wrong!")) | ||
//os.Exit(-1) | ||
//fmt.Println("End") | ||
} |
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 client | ||
|
||
import ( | ||
"ch15/series" | ||
"testing" | ||
) | ||
|
||
func TestPackage(t *testing.T) { | ||
t.Log(series.GetFibonacciSerie(5)) | ||
t.Log(series.Square(5)) | ||
} |
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,13 @@ | ||
package remote | ||
|
||
import ( | ||
"testing" | ||
|
||
cm "github.com/easierway/concurrent_map" | ||
) | ||
|
||
func TestConcurrentMap(t *testing.T) { | ||
m := cm.CreateConcurrentMap(99) | ||
m.Set(cm.StrKey("key"), 10) | ||
t.Log(m.Get(cm.StrKey("key"))) | ||
} |
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,23 @@ | ||
package series | ||
|
||
import "fmt" | ||
|
||
func init() { | ||
fmt.Println("init1") | ||
} | ||
|
||
func init() { | ||
fmt.Println("init2") | ||
} | ||
|
||
func Square(n int) int { | ||
return n * n | ||
} | ||
|
||
func GetFibonacciSerie(n int) []int { | ||
ret := []int{1, 1} | ||
for i := 2; i < n; i++ { | ||
ret = append(ret, ret[i-2]+ret[i-1]) | ||
} | ||
return ret | ||
} |
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,17 @@ | ||
package groutine_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestGroutine(t *testing.T) { | ||
for i := 0; i < 10; i++ { | ||
go func(i int) { | ||
//time.Sleep(time.Second * 1) | ||
fmt.Println(i) | ||
}(i) | ||
} | ||
time.Sleep(time.Millisecond * 50) | ||
} |
Oops, something went wrong.