英文:
Go runtime error: panic: runtime error: invalid memory address or nil pointer dereference
问题
我的代码在几秒钟内正常运行,但之后会抛出一个错误:
panic: 运行时错误:无效的内存地址或空指针解引用
panic: 运行时错误:无效的内存地址或空指针解引用 [信号 SIGSEGV: 分段违规 code=0x1
addr=0x328 pc=0x58d086]goroutine 1884 [正在运行]: crypto/tls.(*Conn).Close(0x0, 0x0, 0x0)
/usr/local/go/src/crypto/tls/conn.go:1310 +0x26 panic(0x5dcc60, 0x73dc30)
/usr/local/go/src/runtime/panic.go:965 +0x1b9 crypto/tls.(*Conn).Write(0x0, 0xc000701fb0, 0x23, 0x30, 0x0, 0x0, 0x0)
/usr/local/go/src/crypto/tls/conn.go:1101 +0x66 main.main.func1(0xc0000aa270, 0xd, 0x60d313, 0x9)
/home/nt-user/workspace/Main.go:28 +0x19b created by main.main
/home/nt-user/workspace/Main.go:18 +0xbf exit status 2
package main
import (
// "net"
"time"
"fmt"
"crypto/tls"
)
func main() {
var (
targetHost = "网站URL"
targetPort = "443"
targetURL = targetHost + ":" + targetPort
)
for true {
for i := 0; i < 500; i++ {
go func() {
var s, err = tls.Dial("tcp", targetURL, &tls.Config {
ServerName: targetHost,
InsecureSkipVerify: true,
})
if err != nil {
fmt.Println("连接到" + targetHost + "时出错")
}
defer s.Close()
for i := 0; i < 100; i++ {
s.Write([]byte("GET / HTTP/1.1\r\nHost: " + targetHost + "\r\n\r\n"))
}
}()
}
time.Sleep(1 * time.Second)
}
}
我该如何修复这个问题?
英文:
My code run normaly in a few second but after that, it throws an error:
> panic: runtime error: invalid memory address or nil pointer
> dereference
>
> panic: runtime error: invalid memory address or nil pointer
> dereference [signal SIGSEGV: segmentation violation code=0x1
> addr=0x328 pc=0x58d086]
>
> goroutine 1884 [running]: crypto/tls.(*Conn).Close(0x0, 0x0, 0x0)
> /usr/local/go/src/crypto/tls/conn.go:1310 +0x26 panic(0x5dcc60, 0x73dc30)
> /usr/local/go/src/runtime/panic.go:965 +0x1b9 crypto/tls.(*Conn).Write(0x0, 0xc000701fb0, 0x23, 0x30, 0x0, 0x0, 0x0)
> /usr/local/go/src/crypto/tls/conn.go:1101 +0x66 main.main.func1(0xc0000aa270, 0xd, 0x60d313, 0x9)
> /home/nt-user/workspace/Main.go:28 +0x19b created by main.main
> /home/nt-user/workspace/Main.go:18 +0xbf exit status 2
package main
import (
//"net"
"time"
"fmt"
"crypto/tls"
)
func main() {
var (
targetHost = "Website URL"
targetPort = "443"
targetURL = targetHost + ":" + targetPort
)
for true {
for i := 0; i < 500; i++ {
go func() {
var s, err = tls.Dial("tcp", targetURL, &tls.Config {
ServerName: targetHost,
InsecureSkipVerify: true,
})
if err != nil {
fmt.Println("Error when connect to " + targetHost)
}
defer s.Close()
for i := 0; i < 100; i++ {
s.Write([]byte("GET / HTTP/1.1\r\nHost: " + targetHost + "\r\n\r\n"))
}
}()
}
time.Sleep(1 * time.Second)
}
}
How can I fix it?
答案1
得分: 1
在你的go func中,在tls.Dial
之后,如果出现错误,它将继续使用s.Write
中的nil s
。这就是为什么会发生panic。所以如果有错误,就返回。
var s, err = tls.Dial("tcp", targetURL, &tls.Config {
ServerName: targetHost,
InsecureSkipVerify: true,
})
if err != nil {
fmt.Println("连接到" + targetHost + "时出错")
return //在这里返回,否则它会继续使用nil "s"
}
我建议不要打印错误。你可以将错误记录在你喜欢的日志记录器中,或者将其发送到一个错误通道并单独处理错误。
英文:
In your go func, after tls.Dial
, if it is getting error, it will continue and use nil s
in s.Write
. That's why panic happens. So return if there is an error.
var s, err = tls.Dial("tcp", targetURL, &tls.Config {
ServerName: targetHost,
InsecureSkipVerify: true,
})
if err != nil {
fmt.Println("Error when connect to " + targetHost)
return //return here, otherwise it continues to use nil "s"
}
I prefer not to print error. Log it in your preferred logger or send it to an error channel and handle errors separately.
答案2
得分: -1
让我知道这是否对你有帮助。
for i := 0; i < 500; i++ {
go func() {
var s, err = tls.Dial("tcp", targetURL, &tls.Config {
ServerName: targetHost,
InsecureSkipVerify: true,
})
defer s.Close() // 移动到这里
if err != nil {
fmt.Println("连接到" + targetHost + "时出错")
return
}
for i := 0; i < 100; i++ {
s.Write([]byte("GET / HTTP/1.1\r\nHost: " + targetHost + "\r\n\r\n"))
}
}()
time.Sleep(1 * time.Second) // 延迟一段时间
}
英文:
Let me know if this helps you
for i := 0; i < 500; i++ {
go func() {
var s, err = tls.Dial("tcp", targetURL, &tls.Config {
ServerName: targetHost,
InsecureSkipVerify: true,
})
defer s.Close() // moved to here
if err != nil {
fmt.Println("Error when connect to " + targetHost)
return
}
for i := 0; i < 100; i++ {
s.Write([]byte("GET / HTTP/1.1\r\nHost: " + targetHost + "\r\n\r\n"))
}
}()
time.Sleep(1 * time.Second) // give some delay
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论