英文:
Is there a dead lock in the following go code? Not output though
问题
我正在运行以下的Go代码,但是它没有产生输出:
package main
import "fmt"
import "time"
func Wait(){
time.Sleep(2000 * time.Millisecond)
}
func Print(c chan string){
fmt.Println("Running Print go-routine")
for{
fmt.Println("len(c): ", len(c))
str := <- c
fmt.Println(str)
}
}
func main() {
c := make(chan string, 4)
c <- "0"
c <- "1"
c <- "2"
c <- "3"
Wait()
fmt.Println("Before go Print(c)")
go Print(c)
fmt.Println("After go Print(c)")
}
是否发生了死锁?Print(c)
函数甚至没有被调用...?这对我来说非常奇怪。
在Go Playground中的链接是:http://play.golang.org/p/tDjEJKwkRJ
英文:
I am running the following go code and it doesn't produce output:
package main
import "fmt"
//import "strconv"
import "time"
func Wait(){
time.Sleep(2000 * time.Millisecond)
}
func Print(c chan string){
fmt.Println("Running Print go-routine")
for{
fmt.Println("len(c): ", len(c))
str := <- c
fmt.Println(str)
}
}
func main() {
c := make(chan string, 4)
c <- "0"
c <- "1"
c <- "2"
c <- "3"
Wait()
fmt.Println("Before go Print(c)")
go Print(c)
fmt.Println("After go Print(c)")
}
Is there a deadlock? The Print(c)
function is not even called...? Its very strange to me.
The link to it in go playground is: http://play.golang.org/p/tDjEJKwkRJ
答案1
得分: 2
没有错误,Print()函数在一个goroutine中被调用,但是主程序在此之后立即退出...所以goroutine被终止。
阅读这个讲座:Go并发模式(或者更好地,观看它的视频),以了解通道和goroutine的工作原理。
请记住,当主函数返回时,程序已经结束。
英文:
There isn't an error, the Print() function is called in a goroutine, but the main program is exiting right after that... so the goroutine is terminated.
Read this talk: Go Concurrency Patterns (or better, view it's video), to understand how channels and goroutines work.
Keep in mind that the program is finished when the main function returns.
答案2
得分: 2
当main
函数调用返回时,程序退出。它不会等待其他(非main
)goroutine 完成。
在go Print(c)
语句之后,稍后调用你的Wait
函数。例如,
package main
import (
"fmt"
"time"
)
func Wait() {
time.Sleep(2000 * time.Millisecond)
}
func Print(c chan string) {
fmt.Println("Running Print go-routine")
for {
fmt.Println("len(c): ", len(c))
str := <-c
fmt.Println(str)
}
}
func main() {
c := make(chan string, 4)
c <- "0"
c <- "1"
c <- "2"
c <- "3"
fmt.Println("Before go Print(c)")
go Print(c)
Wait()
fmt.Println("After go Print(c)")
}
输出:
Before go Print(c)
Running Print go-routine
len(c): 4
0
len(c): 3
1
len(c): 2
2
len(c): 1
3
len(c): 0
After go Print(c)
通过将一个名为main的单个未导入的包与其导入的所有包进行链接,可以创建一个完整的程序,其中包括传递性。主包必须具有包名main,并声明一个不带参数且不返回值的main函数。
func main() { … }
程序的执行从初始化主包开始,然后调用main函数。当该函数调用返回时,程序退出。它不会等待其他(非main)goroutine 完成。
英文:
When the main
function invocation returns, the program exits. It does not wait for other (non-main
) goroutines to complete.
Call your Wait
function later, after the go Print(c)
statement. For example,
package main
import (
"fmt"
"time"
)
func Wait() {
time.Sleep(2000 * time.Millisecond)
}
func Print(c chan string) {
fmt.Println("Running Print go-routine")
for {
fmt.Println("len(c): ", len(c))
str := <-c
fmt.Println(str)
}
}
func main() {
c := make(chan string, 4)
c <- "0"
c <- "1"
c <- "2"
c <- "3"
fmt.Println("Before go Print(c)")
go Print(c)
Wait()
fmt.Println("After go Print(c)")
}
Output:
Before go Print(c)
Running Print go-routine
len(c): 4
0
len(c): 3
1
len(c): 2
2
len(c): 1
3
len(c): 0
After go Print(c)
> The Go Programming Language Specification
>
> Program execution
>
> A complete program is created by linking a single, unimported package
> called the main package with all the packages it imports,
> transitively. The main package must have package name main and declare
> a function main that takes no arguments and returns no value.
>
> func main() { … }
>
> Program execution begins by initializing the main package and then
> invoking the function main. When that function invocation returns, the
> program exits. It does not wait for other (non-main) goroutines to
> complete.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论