为什么此代码不返回“死锁"?错误? [英] Why doesn't this code return a "deadlock" error?

查看:85
本文介绍了为什么此代码不返回“死锁"?错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package main

import (
    "fmt"
    "net/http"
)

func Extract(url string) ([]string, error) {
    http.Get(url)

    var links []string
    return links, nil
}

func crawl(url string) []string {
    list, _ := Extract(url)
    return list
}

func main() {
    var ch = make(chan int)
    ch <- 1
}

如果我删除了net/http导入,它将按预期返回死锁"错误.但是,如果我导入此程序包,尽管我没有调用Extract函数,但死锁"将不会出现.

If I remove the net/http import, it will return a "deadlock" error as expected. But if I import this package, although I didn't invoke the Extract func, the "deadlock" will not appear.

推荐答案

导入网络程序包将启动后台轮询Goroutine,从而有效禁用死锁检测器.

Importing the net package starts background polling Goroutines that effectively disable the deadlock detector.

您可以在此处查看有关类似问题的讨论: https://github.com/golang /go/issues/12734

You can see the discussion for a similar issue here: https://github.com/golang/go/issues/12734

这篇关于为什么此代码不返回“死锁"?错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆