dp13668681869 2018-06-05 14:20
浏览 158

无论如何执行goroutine时,所有goroutine都处于睡眠状态

I'm receiving the following error and I don't understand why:

Send: Query Herefatal error: all goroutines are asleep - deadlock!

You can see im calling a function routine i created using goroutine. I really dont have any more details to provide.

package main

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

const (
    udphost       = "127.0.0.1"
    udpport       = ":150"
    StopCharacter = "

"
)

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() 
}
  • 写回答

1条回答 默认 最新

  • douhuang5331 2018-06-05 15:27
    关注

    Try

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?