Skip to content

Commit

Permalink
Added new demos
Browse files Browse the repository at this point in the history
  • Loading branch information
chaocai committed Mar 18, 2019
1 parent 54becad commit 946fa8f
Show file tree
Hide file tree
Showing 16 changed files with 528 additions and 0 deletions.
28 changes: 28 additions & 0 deletions code/ch11/customer_type/customer_type_test.go
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))
}
44 changes: 44 additions & 0 deletions code/ch11/encapsulation/encap_test.go
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())
}
20 changes: 20 additions & 0 deletions code/ch11/interface/interface_test.go
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())
}
32 changes: 32 additions & 0 deletions code/ch12/extension/extension_test.go
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")
}
31 changes: 31 additions & 0 deletions code/ch13/empty_interface/empty_interface_test.go
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")
}
36 changes: 36 additions & 0 deletions code/ch13/polymorphsim/polymorphsim_test.go
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)
}
74 changes: 74 additions & 0 deletions code/ch14/error/err_test.go
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)

}
20 changes: 20 additions & 0 deletions code/ch14/panic_recover/panic_recover_test.go
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")
}
11 changes: 11 additions & 0 deletions code/ch15/client/package_test.go
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))
}
13 changes: 13 additions & 0 deletions code/ch15/remote_package/remote_package_test.go
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")))
}
23 changes: 23 additions & 0 deletions code/ch15/series/my_series.go
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
}
17 changes: 17 additions & 0 deletions code/ch16/groutine/groutine_test.go
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)
}
Loading

0 comments on commit 946fa8f

Please sign in to comment.