Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 执行HTTP请求时出错_Go - Fatal编程技术网

Go 执行HTTP请求时出错

Go 执行HTTP请求时出错,go,Go,当运行以下http客户端对web服务器进行压力测试时,出现了奇怪的错误,不知道原因是什么。go的版本是go1.8.1Linux/amd64,运行在Ubuntu14.04上,内存为16GB $ go run te2.go 563.904492ms Get http://10.3.0.6/small: dial tcp 10.3.0.6:80: connect: cannot assign requested address Get http://10.3.0.6/small: dial tcp

当运行以下http客户端对web服务器进行压力测试时,出现了奇怪的错误,不知道原因是什么。go的版本是
go1.8.1Linux/amd64
,运行在Ubuntu14.04上,内存为16GB

$ go run te2.go 
563.904492ms
Get http://10.3.0.6/small: dial tcp 10.3.0.6:80: connect: cannot assign requested address
Get http://10.3.0.6/small: dial tcp 10.3.0.6:80: connect: cannot assign requested address
Get http://10.3.0.6/small: dial tcp 10.3.0.6:80: connect: cannot assign requested address
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x5e3b27]

goroutine 140284 [running]:
main.httpGet(0x0)
    /home/jon/learn/go/te2.go:27 +0x107
created by main.main
    /home/jon/learn/go/te2.go:45 +0xd3
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x5e3b27]

goroutine 140375 [running]:
main.httpGet(0x0)
    /home/jon/learn/go/te2.go:27 +0x107
created by main.main
    /home/jon/learn/go/te2.go:45 +0xd3
exit status 2
有什么想法吗

以下是代码片段:

package main

import "fmt"
import "bufio"
import "os"
import "time"
import "net/http"

var req = []byte("GET /small HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"Content-Length: 0\r\n\r\n");
var buf = make([]byte, 1024)
var total = 0;
var t0 = time.Now()
var c = make(chan int)

func httpGet ()  int {
    req, err := http.NewRequest("GET", "http://10.3.0.6/small", nil)
    //req.Header.Add("User-Agent", `MYCLIENT`)
    //req.Header.Add("Cookie", `sessid=12345`)
    client := &http.Client{}
    resp, err := client.Do(req)
    //defer resp.Body.Close()
    if (err != nil) {
        fmt.Println(err)
    }
    resp.Body.Close()
    total ++
    if (total == 10000) {
        fmt.Println(time.Now().Sub(t0))
    }
    c <- 1
    return 0;
}

func main() {
    i := 1
    t0 = time.Now()
    for (i < 1000) {
        go httpGet()
        i += 1
    }
    for (1 < 2) {
        <-c
        go httpGet()
    }
    reader := bufio.NewReader(os.Stdin)
    text, _ := reader.ReadString('\n')
    fmt.Println(text)   
}
主程序包
输入“fmt”
输入“bufio”
导入“操作系统”
导入“时间”
导入“net/http”
var req=[]字节(“GET/small HTTP/1.1\r\n”+
“主机:本地主机\r\n”+
“内容长度:0\r\n\r\n”);
var buf=make([]字节,1024)
var合计=0;
var t0=time.Now()
变量c=制造(成交量)
func httpGet()int{
req,err:=http.NewRequest(“GET”http://10.3.0.6/small“,无)
//添加(“用户代理”,“我的客户机”)
//添加(“Cookie”,sessiond=12345`)
客户端:=&http.client{}
resp,err:=client.Do(请求)
//延迟响应主体关闭()
如果(错误!=nil){
fmt.Println(错误)
}
各自主体关闭()
总数++
如果(总数=10000){
fmt.Println(time.Now().Sub(t0))
}
C
在这个片段中,您将检查
err
并继续执行

因此,如果发生了什么事情,
err
不是
nil
,而是
resp
是(或
resp.Body
是)-您打印错误,然后取消引用nil指针


您应该做的事情:如果发生错误-清除并从函数返回。

无关:在
total
变量上存在数据争用。使用
atomic。下面的AddInt32
回答对于死机是正确的。另一条消息
connect:cannotassign requested address
我认为是由于到u的端口用完了se发出请求。@zerkms,感谢您使用
原子。AddInt32
,这非常有意义。感谢@Adrian提供的提示,我设置了
ip\u local\u port\u范围
,但没有多大帮助。但是在设置
conn.(*net.TCPConn)。SetLinger(0)
,这很有帮助。我想区别在于将linger选项设置为0将强制TCP会话在结束时重置,而不是自然超时。
resp, err := client.Do(req)
//defer resp.Body.Close()
if (err != nil) {
    fmt.Println(err)
}
resp.Body.Close()