forked from rubyhan1314/Golang-100-Days
-
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
1 parent
f73d2c3
commit 68ed6ec
Showing
36 changed files
with
1,644 additions
and
187 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,22 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
/* | ||
channel,通道 | ||
*/ | ||
var a chan int | ||
fmt.Printf("%T,%v\n",a,a) | ||
|
||
if a == nil{ | ||
fmt.Println("channel是nil的,不能使用,需要先创建通道。。") | ||
a = make(chan int) | ||
fmt.Println(a) | ||
} | ||
test1(a) | ||
} | ||
|
||
func test1(ch chan int){ | ||
fmt.Printf("%T,%v\n",ch,ch) | ||
} |
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,22 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
var ch1 chan bool | ||
ch1 = make(chan bool) | ||
|
||
go func() { | ||
for i:=0;i<10;i++{ | ||
fmt.Println("子goroutine中,i:",i) | ||
} | ||
//循环结束后,向通道中写数据,表示要结束了。。 | ||
ch1 <- true | ||
fmt.Println("结束。。") | ||
}() | ||
|
||
|
||
data := <-ch1 | ||
fmt.Println("main...data-->",data) | ||
fmt.Println("main...over...") | ||
} |
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,25 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func main() { | ||
ch1 := make(chan int) | ||
|
||
go func() { | ||
fmt.Println("子goroutine开始执行。。") | ||
//time.Sleep(3 * time.Second) | ||
data := <-ch1 //从ch1中读取数据 | ||
fmt.Println("data:",data) | ||
}() | ||
|
||
time.Sleep(5*time.Second) | ||
ch1 <- 10 | ||
fmt.Println("main..over...") | ||
|
||
|
||
ch := make(chan int) | ||
ch <- 100 //阻塞 | ||
} |
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 main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func main() { | ||
/* | ||
关闭通道:close(ch) | ||
子goroutine:写出10个数据 | ||
每写一个,阻塞一次,主goroutine读取一次,解除阻塞 | ||
主goroutine,读取数据 | ||
每次读取数据,阻塞一次,子goroutine,写出一个,解除阻塞 | ||
*/ | ||
ch1 := make(chan int) | ||
go sendData(ch1) | ||
|
||
//读取通道的数据 | ||
for{ | ||
time.Sleep(1*time.Second) | ||
v, ok := <- ch1 // 最后一次读取 | ||
if !ok{ | ||
fmt.Println("已经读取了所有的数据。。",ok,v) | ||
break | ||
} | ||
fmt.Println("读取的数据:",v,ok) | ||
} | ||
|
||
fmt.Println("main..over...") | ||
|
||
} | ||
|
||
func sendData(ch1 chan int){ | ||
//发送方:10条数据 | ||
for i:=0;i<10;i++{ | ||
ch1 <- i //将i写入到通道中 | ||
// 0 1 .. 9 | ||
} | ||
close(ch1) //将ch1通道关闭 | ||
} | ||
|
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 main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func main() { | ||
/* | ||
通过range访问通道 | ||
*/ | ||
ch1 := make(chan int) | ||
go sendData(ch1) | ||
//for循环的for range,来访问通道 | ||
for v := range ch1{ // v <- ch1 | ||
fmt.Println("读取数据:",v) | ||
} | ||
fmt.Println("main..over...") | ||
} | ||
func sendData(ch1 chan int){ | ||
for i:=0;i<10;i++{ | ||
time.Sleep(1* time.Second) | ||
ch1 <- i // 0 1...9 | ||
} | ||
close(ch1)//通知对方,通道关闭 | ||
} |
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,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
func main() { | ||
/* | ||
非缓冲通道:make(chan T) | ||
一次发送,一次接收,都是阻塞的 | ||
缓冲通道:make(chan T , capacity) | ||
发送:缓冲区的数据满了,才会阻塞 | ||
接收:缓冲区的数据空了,才会阻塞 | ||
*/ | ||
ch1 := make(chan int) //非缓冲通道 | ||
fmt.Println(len(ch1),cap(ch1)) //0 0 | ||
//ch1 <- 100 //阻塞式的,需要有其他的goroutine解除阻塞,否则deadlock | ||
|
||
ch2 := make(chan int, 5) //缓冲通道,缓冲区大小是5 | ||
fmt.Println(len(ch2),cap(ch2)) //0 5 | ||
|
||
ch2 <- 100 | ||
fmt.Println(len(ch2),cap(ch2)) //1 5 | ||
ch2 <- 200 | ||
ch2 <- 300 | ||
ch2 <- 400 | ||
ch2 <- 500 | ||
fmt.Println(len(ch2),cap(ch2)) //5 5 | ||
//ch2 <- 600 | ||
|
||
fmt.Println("-----------------") | ||
ch3 := make(chan string, 4) | ||
go sendData(ch3) | ||
|
||
for{ | ||
v, ok := <-ch3 | ||
if !ok{ | ||
fmt.Println("读完了。。",ok) | ||
break | ||
} | ||
fmt.Println("\t读取的数据是:",v) | ||
} | ||
fmt.Println("main。。over。。。。") | ||
|
||
} | ||
|
||
func sendData(ch chan string){ | ||
for i:= 0;i<10;i++{ | ||
ch <- "数据" + strconv.Itoa(i) | ||
fmt.Printf("子goroutine中写出第 %d 个数据\n",i) | ||
} | ||
close(ch) | ||
} |
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,38 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
/* | ||
双向: | ||
chan T | ||
chan <- data,发送数据,写出 | ||
data <- chan,获取数据,读取 | ||
单向:定向 | ||
chan <- T,只支持写 | ||
<- chan T,只读 | ||
*/ | ||
ch1 := make(chan string) | ||
done := make(chan bool) | ||
go sendData(ch1,done) | ||
|
||
data := <- ch1 //读取 | ||
fmt.Println("子goroutine传来:",data) | ||
|
||
ch1 <- "我是main" //发送 | ||
|
||
<- done | ||
fmt.Println("main..over...") | ||
} | ||
|
||
func sendData(ch1 chan string,done chan bool){ | ||
ch1 <- "我是韩茹" //发送 | ||
|
||
data := <- ch1 //读取 | ||
fmt.Println("main goroutine传来:",data) | ||
|
||
|
||
done <- true | ||
|
||
} |
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,53 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
/* | ||
单向:定向 | ||
chan <- T,只支持写 | ||
<- chan T,只读 | ||
定向通道: | ||
双向:-->函数:只读,只写 | ||
*/ | ||
ch1 := make(chan int) //双向,读,写 | ||
//ch2 := make(chan <- int) //单向,只能写,不能读 | ||
//ch3 := make(<- chan int) //单向,只能读,不能写 | ||
|
||
//ch1 <- 100 | ||
//data:=<-ch1 | ||
//ch2 <- 1000 | ||
//data := <- ch2 //invalid operation: <-ch2 (receive from send-only type chan<- int) | ||
//data := <- ch3 | ||
//ch3 <- 2000 //invalid operation: ch3 <- 2000 (send to receive-only type <-chan int) | ||
|
||
|
||
go fun1(ch1) //可读,可写 | ||
//go fun1(ch2) //只写 | ||
|
||
data := <- ch1 | ||
fmt.Println("fun1函数中写出的数据是:",data) | ||
|
||
go fun2(ch1) | ||
//fun2(ch3) | ||
|
||
ch1 <- 200 | ||
fmt.Println("main..over...") | ||
} | ||
|
||
//该函数,只能操作只写的通道 | ||
func fun1(ch chan <- int){ | ||
//在函数内部,对于ch1通道,只能写数据,不能读取数据 | ||
ch <- 100 | ||
fmt.Println("fun1函数结束。。。") | ||
} | ||
|
||
//该函数,只能操作只读的通道 | ||
func fun2(ch <- chan int){ | ||
data := <-ch | ||
fmt.Println("fun2函数,从ch中读取的数据是:",data) | ||
} | ||
|
||
|
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,37 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
/* | ||
1. func NewTimer(d Duration) *Timer | ||
创建一个计时器,d时间以后触发 | ||
*/ | ||
//timer := time.NewTimer(3 *time.Second) | ||
//fmt.Printf("%T\n",timer) //*time.Timer | ||
//fmt.Println(time.Now()) //2019-08-15 11:32:17.065452 +0800 CST m=+0.000614404 | ||
// | ||
////此处等待channel中的数值,会阻塞3秒 | ||
//ch2 := timer.C | ||
//fmt.Println(<-ch2 ) //2019-08-15 11:32:20.068101 +0800 CST m=+3.003327715 | ||
|
||
|
||
//新建一个计时器 | ||
timer2 := time.NewTimer(5*time.Second) | ||
//开始goroutine,来处理触发后的事件 | ||
go func() { | ||
<- timer2.C | ||
fmt.Println("Timer 2 结束了。。。开始。。。。") | ||
}() | ||
|
||
time.Sleep(3*time.Second) | ||
flag := timer2.Stop() | ||
if flag{ | ||
fmt.Println("Timer 2 停止了。。。") | ||
} | ||
} |
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,22 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
/* | ||
2. func After(d Duration) <-chan Time | ||
返回一个通道:chan,存储的是d时间间隔之后的当前时间 | ||
相当于:return NewTimer(d).C | ||
*/ | ||
ch := time.After(3 *time.Second) | ||
fmt.Printf("%T\n",ch) //<-chan time.Time | ||
fmt.Println(time.Now()) //2019-08-15 11:43:33.941039 +0800 CST m=+0.000537462 | ||
|
||
|
||
time2 := <-ch | ||
fmt.Println(time2) //2019-08-15 11:43:36.945775 +0800 CST m=+3.005338577 | ||
} |
Oops, something went wrong.