Skip to content

Commit

Permalink
20181224
Browse files Browse the repository at this point in the history
  • Loading branch information
eaok committed Jan 12, 2019
1 parent e5b46fd commit fd74e78
Show file tree
Hide file tree
Showing 65 changed files with 1,148 additions and 616 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,17 @@ Training for golang.
# 学习资料
1 [《Go语言标准库》The Golang Standard Library by Example][1]

2 [Go语言核心编程][2]

3 [Go语言编程入门与实战技巧][3]

4 [Go并发编程实战(第2版)][4]

5 [Go语言实战][5]


[1]: https://books.studygolang.com/The-Golang-Standard-Library-by-Example/
[2]: http://www.broadview.com.cn/book/5522
[3]: http://www.broadview.com.cn/book/5389
[4]: https://item.jd.com/12063141.html
[5]: https://item.jd.com/12136974.html
28 changes: 14 additions & 14 deletions interview/01.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
package main

import (
"fmt"
"fmt"
)

func main() {
defer_call()
func main() {
defer_call()
}

func defer_call() {
defer func() {
fmt.Println("打印前")
}()
func defer_call() {
defer func() {
fmt.Println("打印前")
}()

defer func() {
// if err := recover(); err != nil {
// fmt.Println(err) //err就是panic
// }
fmt.Println("打印中")
}()
defer func() {
// if err := recover(); err != nil {
// fmt.Println(err) //err就是panic
// }
fmt.Println("打印中")
}()

defer func() {
defer func() {
fmt.Println("打印后")
}()

Expand Down
54 changes: 27 additions & 27 deletions interview/02.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@ package main
import "fmt"

type student struct {
Name string
Age int
Name string
Age int
}

func pase_student() map[string]*student {
m := make(map[string]*student)
stus := []student{
{"zhou", 24},
{"li", 23},
{"wang", 22},
}
m := make(map[string]*student)
stus := []student{
{"zhou", 24},
{"li", 23},
{"wang", 22},
}

for _, stu := range stus {
// fmt.Println(stu)
// println(&stu)
m[stu.Name] = &stu
}
for _, stu := range stus {
// fmt.Println(stu)
// println(&stu)
m[stu.Name] = &stu
}

// 正确
// for i:=0;i<len(stus);i++ {
// m[stus[i].Name] = &stus[i]
// }
// for i, _ := range stus {
// stu := stus[i]//这里直接用key来取value赋值到stu变量中,这样stu的地址都是新的。
// m[stu.Name] = &stu
// }
// 正确
// for i:=0;i<len(stus);i++ {
// m[stus[i].Name] = &stus[i]
// }
// for i, _ := range stus {
// stu := stus[i]//这里直接用key来取value赋值到stu变量中,这样stu的地址都是新的。
// m[stu.Name] = &stu
// }

return m
return m
}

func main() {
students := pase_student()
fmt.Println(students)
for k, v := range students {
fmt.Printf("key=%s,value=%v\n", k, v)
}
students := pase_student()
fmt.Println(students)
for k, v := range students {
fmt.Printf("key=%s,value=%v\n", k, v)
}
}
14 changes: 7 additions & 7 deletions interview/04.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import (
type People struct{}

func (p *People) ShowA() {
fmt.Println("showA")
p.ShowB()
fmt.Println("showA")
p.ShowB()
}
func (p *People) ShowB() {
fmt.Println("showB")
fmt.Println("showB")
}

type Teacher struct {
People
People
}

func (t *Teacher) ShowB() {
fmt.Println("teacher showB")
fmt.Println("teacher showB")
}

func main() {
t := Teacher{}
t.ShowA()
t := Teacher{}
t.ShowA()
}
22 changes: 11 additions & 11 deletions interview/05.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
)

func main() {
runtime.GOMAXPROCS(1)
int_chan := make(chan int, 1)
string_chan := make(chan string, 1)
int_chan <- 1
string_chan <- "hello"
select {
case value := <-int_chan:
fmt.Println(value)
case value := <-string_chan:
panic(value)
}
runtime.GOMAXPROCS(1)
int_chan := make(chan int, 1)
string_chan := make(chan string, 1)
int_chan <- 1
string_chan <- "hello"
select {
case value := <-int_chan:
fmt.Println(value)
case value := <-string_chan:
panic(value)
}
}
18 changes: 9 additions & 9 deletions interview/06.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
)

func calc(index string, a, b int) int {
ret := a + b
fmt.Println(index, a, b, ret)
return ret
ret := a + b
fmt.Println(index, a, b, ret)
return ret
}

func main() {
a := 1
b := 2
defer calc("1", a, calc("10", a, b))
a = 0
defer calc("2", a, calc("20", a, b))
a := 1
b := 2
defer calc("1", a, calc("10", a, b))
a = 0
defer calc("2", a, calc("20", a, b))
a = 3
b = 1
b = 1
}
8 changes: 4 additions & 4 deletions interview/07.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
package main

import (
"fmt"
"fmt"
)

func main() {
s := make([]int, 5)
s = append(s, 1, 2, 3)
fmt.Println(s)
s := make([]int, 5)
s = append(s, 1, 2, 3)
fmt.Println(s)
}
14 changes: 7 additions & 7 deletions interview/08.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
package main

import (
"fmt"
"fmt"
"sync"
"time"
"time"
)

type UserAges struct {
Expand Down Expand Up @@ -32,15 +32,15 @@ func main() {

go func() {
for i := 0; i < 1000; i++ {
a.Add("TEST", 12)
a.Add("TEST", 12)
}
}()

go func() {
go func() {
for i := 0; i < 1000; i++ {
fmt.Println(a.Get("TEST"))
fmt.Println(a.Get("TEST"))
}
}()
}()

time.Sleep(time.Hour)
time.Sleep(time.Hour)
}
44 changes: 22 additions & 22 deletions interview/09.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@
package main

import (
"sync"
"fmt"
"fmt"
"sync"
)

type threadSafeSet struct {
sync.RWMutex
s []interface{}
sync.RWMutex
s []interface{}
}

func (set *threadSafeSet) Iter() <-chan interface{} {
//ch := make(chan interface{}) // 解除注释看看!
ch := make(chan interface{},len(set.s))
go func() {
set.RLock()
//ch := make(chan interface{}) // 解除注释看看!
ch := make(chan interface{}, len(set.s))
go func() {
set.RLock()

for elem,value := range set.s {
ch <- elem
println("Iter:",elem,value)
}
for elem, value := range set.s {
ch <- elem
println("Iter:", elem, value)
}

close(ch)
set.RUnlock()
}()
return ch
close(ch)
set.RUnlock()
}()
return ch
}

func main() {
th:=threadSafeSet{
s:[]interface{}{"1","2", "3", "4", "5", "6", "7", "8"},
}
v:=<-th.Iter()
fmt.Sprintf("%s%v","ch",v)
func main() {
th := threadSafeSet{
s: []interface{}{"1", "2", "3", "4", "5", "6", "7", "8"},
}
v := <-th.Iter()
fmt.Sprintf("%s%v", "ch", v)
}
2 changes: 1 addition & 1 deletion interview/10.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type People interface {
type Stduent struct{}

func (stu *Stduent) Speak(think string) (talk string) {
//func (stu Stduent) Speak(think string) (talk string) {
//func (stu Stduent) Speak(think string) (talk string) {
if think == "bitch" {
talk = "You are a good boy"
} else {
Expand Down
10 changes: 5 additions & 5 deletions interview/15.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
package main

import (
"fmt"
"fmt"
)

func main() {
list := new([]int)
// list := make([]int, 0)
list = append(list, 1)
fmt.Println(list)
list := new([]int)
// list := make([]int, 0)
list = append(list, 1)
fmt.Println(list)
}
2 changes: 1 addition & 1 deletion interview/16.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ func main() {
s1 := []int{1, 2, 3}
s2 := []int{4, 5}
s1 = append(s1, s2)
// s1 = append(s1, s2...)
// s1 = append(s1, s2...)
fmt.Println(s1)
}
12 changes: 6 additions & 6 deletions interview/17.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main

import (
"fmt"
"reflect"
"reflect"
)

func main() {
Expand Down Expand Up @@ -34,11 +34,11 @@ func main() {
if sm1 == sm2 {
fmt.Println("sm1 == sm2")
}
// if reflect.DeepEqual(sm1, sm2) {
// fmt.Println("sm1 == sm2")
// } else {
// fmt.Println("sm1 == sm2")
// }
// if reflect.DeepEqual(sm1, sm2) {
// fmt.Println("sm1 == sm2")
// } else {
// fmt.Println("sm1 == sm2")
// }
}

//结构体属性中有不可以比较的类型,如map,slice。不能比较
Loading

0 comments on commit fd74e78

Please sign in to comment.