gpt4 book ai didi

go - 反正执行 goroutine 时所有 goroutines 都睡着了

转载 作者:数据小太阳 更新时间:2023-10-29 03:42:51 31 4
gpt4 key购买 nike

我收到以下错误,我不明白为什么:

发送:查询 Herefatal 错误:所有 goroutines 都睡着了 - 死锁!

您可以看到我正在调用我使用 goroutine 创建的函数 routine。我真的没有更多细节可以提供。

package main

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

const (
udphost = "127.0.0.1"
udpport = ":150"
StopCharacter = "\r\n\r\n"
)

var wg sync.WaitGroup

func routine() {
defer wg.Done()
// establish connection address parts
schemaUri := udphost + udpport
udpAddr, err := net.ResolveUDPAddr("udp4", schemaUri)

// make connection
conn, err := net.DialUDP("udp", nil, udpAddr)
fmt.Printf("%v", conn)

// defer close
defer conn.Close()

// handle connection errors
if err != nil {
fmt.Println("Erorr Establishing UDP Connection")
return
}

// input query
message := "Query Here"

// Write query to server
conn.Write([]byte(message))
conn.Write([]byte(StopCharacter))
fmt.Printf("Send: %s", message)

// Read response from server
buffr := make([]byte, 1024)
buffw := make([]byte, 1024)
n, _, _, _, _ := conn.ReadMsgUDP(buffr, buffw)

fmt.Printf("Receive: %s", n)

// parse message
msg := string(buffr[0:n])
fmt.Println(msg)
}

func main() {
wg.Add(1)
go routine()
wg.Wait()
}

最佳答案

尝试

func main(){
var wg &sync.WaitGroup
wg.Add(1)
go routine(wg)
...
wg.Wait()
}
func routine(wg *sync.WaitGroup){
...
defer wg.Done()
}

关于go - 反正执行 goroutine 时所有 goroutines 都睡着了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50702463/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com