lab golang study tutorials 03
Sun Jul 25, 2021Last modified: Sun Jul 25, 2021
-
Categories: (directories)
- π Computer Engineering
-
Series: (travel the world of knowledge!)
- π lab golang study
Go tutorials
- ππλ³Έ λ΄μ©μ tour.golang.org μ μλ λ΄μ©μ μ 리ν κΈμ λλ€.
Hello, Go world! 3
codeblocks
goroutines.go
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("goroutine0")
say("main0") // μμ°¨ μ€νλΌλ¦¬λ μμ λ§μΆ°μ μ€νλ¨
say("main1") // goroutine λΌλ¦¬λ λ
립μ μΌλ‘ λΉλκΈ°μμΌλ‘ μ²λ¦¬λ¨ ()
go say("goroutine2") // λμ€μ λμ€λ μμ°¨ μ€νκ³Ό ν¨κ» μ€νλ¨
go say("goroutine1") // μ¬κΈ°μλ say("main2")
say("main2")
}
output
main0
goroutine0
goroutine0
main0
main0
goroutine0
goroutine0
main0
main0
goroutine0
main1
main1
main1
main1
main1
goroutine1
main2
goroutine2
main2
goroutine1
goroutine2
goroutine1
main2
goroutine2
main2
goroutine1
goroutine2
goroutine1
main2
channels.go
package main
import "fmt"
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum // send sum to c
}
func main() {
s := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(s[:len(s)/2], c)
go sum(s[len(s)/2:], c)
x, y := <-c, <-c // receive from c // μλ§ μ€ν κ°λ
?
fmt.Printf("%T, %v \n",c,c)
fmt.Println(x, y, x+y)
}
output
chan int, 0xc000100060
-5 17 12
channels-reverse.go
func main() {
s := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(s[len(s)/2:], c)
go sum(s[:len(s)/2], c)
x, y:= <-c, <-c // receive from c // μλ§ channelμ μ€ν κ°λ
?
fmt.Printf("%T, %v \n",c,c)
fmt.Println(x, y, x+y)
}
output-reverse
chan int, 0xc000062060
17 -5 12
channel-test.go
func main() {
s := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
for index, _ := range s {
go sum(s[index:index+1],c)
}
for i := range c {
fmt.Println(i)
}
}
output-test
2
7
8
4
-9
0
[7]
[2]
[8]
[-9]
[4]
[0]
7
8
2
-9
0
4
0
-9
4
8
2
7
0
2
7
-9
8
4
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
/tmp/sandbox710675921/prog.go:20 +0x193
channels-close-test-1.go
package main
import "fmt"
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum // send sum to c
}
func closechan(c chan int, iter []int) {
for index, _ := range iter {
go sum(iter[index:index+1],c) // μ¬κΈ°κ° λ€λ¦!!
}
close(c)
}
func main() {
s := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go closechan(c,s)
for i := range c {
fmt.Println(i)
}
/*
go sum(s[:len(s)/2], c)
go sum(s[len(s)/2:], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x+y)
*/
}
output
/// goroutine μ°λ©΄ μ무κ²λ μλ¬λ€!?
Program exited.
channels-close-test-1.go
package main
import "fmt"
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum // send sum to c
}
func closechan(c chan int, iter []int) {
for index, _ := range iter {
sum(iter[index:index+1],c) // μ¬κΈ°κ° λ€λ¦!!
}
close(c)
}
func main() {
s := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go closechan(c,s)
for i := range c {
fmt.Println(i)
}
/*
go sum(s[:len(s)/2], c)
go sum(s[len(s)/2:], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x+y)
*/
}
output
/// goroutine μμ°λ©΄ λ¬λ€!?
7
2
8
-9
4
0
λλμ : goroutine μ μ°λ©΄, λ€λ₯Έ execution μ΄λΌκ³ 보λκ² λ§μ κ² κ°λ€.
μ°Έκ³ : close(c) μμ κ³ , goroutine μμΌλ©΄ 7 2 8 -9 4 0
μμΌλ©΄ 0 8 7 2 -9 4
μλλ 곡ν΅μ μΌλ‘ λμ€λ κ²½κ³ λ¬Έ
fatal error: all goroutines are asleep - deadlock! goroutine 1 [chan receive]: main.main() /tmp/sandbox232678665/prog.go:26 +0x14c
test.go
/*
both are possible!
c := make(chan int)
go closechan(c,s)
c := make(chan int, 10)
closechan(c,s)
*/
package main
import "fmt"
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum // send sum to c
}
func closechan(c chan int, iter []int) {
for index, _ := range iter {
sum(iter[index:index+1],c) // μ¬κΈ°κ° λ€λ¦!!
}
close(c)
}
func main() {
s := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go closechan(c,s)
for i := range c {
fmt.Println(i)
}
/*
go sum(s[:len(s)/2], c)
go sum(s[len(s)/2:], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x+y)
*/
}
buffered-channels.go
package main
import "fmt"
func chanfunc(c chan int,integer int) {
c <- integer
}
func main() {
ch := make(chan int)
go chanfunc(ch,1)
go chanfunc(ch,2)
go chanfunc(ch,3)
fmt.Println(<-ch)
fmt.Println(<-ch)
fmt.Println(<-ch)
}
no-buffered-channels-block.go
func main() {
ch := make(chan int)
go chanfunc(ch,1)
chanfunc(ch,2)
fmt.Println(<-ch)
}
no-buffered-channels-block-output
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.chanfunc(...)
/tmp/sandbox977088187/prog.go:5
main.main()
/tmp/sandbox977088187/prog.go:12 +0x87
goroutine 20 [chan send]:
main.chanfunc(0xc00009c060, 0x1)
/tmp/sandbox977088187/prog.go:5 +0x3f
created by main.main
/tmp/sandbox977088187/prog.go:10 +0x65
/// goroutine μμλ
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.chanfunc(...)
/tmp/sandbox473627473/prog.go:5
main.main()
/tmp/sandbox473627473/prog.go:11 +0x65
/// print μμλ
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.chanfunc(...)
/tmp/sandbox473783957/prog.go:5
main.main()
/tmp/sandbox473783957/prog.go:11 +0x57
range-and-close.go
package main
import (
"fmt"
)
func fibonacci(n int, c chan int) {
x, y := 0, 1
for i := 0; i < n; i++ {
c <- x
x, y = y, x+y
}
close(c)
}
func main() {
c := make(chan int, 10)
go fibonacci(cap(c), c)
// fibonacci(10, c) // capacity μ£Όλ©΄ goroutine μμ¨λ κ° λ€μ΄κ°
// go fibonacci(11, c) // goroutine μ°λ©΄ capacity μ΄κ³Όν΄λ κ° λ€μ΄κ°
for i := range c {
fmt.Println(i," (i)")
v, ok:= <-c
fmt.Println(v,ok)
}
v, ok:= <-c
fmt.Println("final check: ",v,ok)
}
range-and-close-output
0 (i)
1 true
1 (i)
2 true
3 (i)
5 true
8 (i)
13 true
21 (i)
34 true
final check: 0 false
channels-close-buffer-size-test.go
package main
import (
"fmt"
"time"
)
func fibonacci(n int, c chan int) {
x, y := 0, 1
for i := 0; i < n; i++ {
c <- x
x, y = y, x+y
}
fmt.Println("gogo")
close(c)
}
func main() {
c := make(chan int, 1)
// go fibonacci(cap(c), c)
// fibonacci(10, c) // capacity μ£Όλ©΄ goroutine μμ¨λ κ° λ€μ΄κ°
go fibonacci(10, c) // goroutine μ°λ©΄ capacity μ΄κ³Όν΄λ κ° λ€μ΄κ°
for i := range c {
fmt.Println(i," (i)")
time.Sleep(1000*time.Millisecond)
}
v, ok:= <-c
fmt.Println("final check: ",v,ok)
}
output
0 (i)
1 (i)
1 (i)
2 (i)
3 (i)
5 (i)
8 (i)
13 (i)
21 (i)
gogo
34 (i)
final check: 0 false