我正在golang中构建一个https客户端,它应该连接到指定的主机并从主机的头部或证书中获取服务器名称。这是我的代码
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
caCert, _ := ioutil.ReadFile("./cacert.pem")
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
conf := &tls.Config{RootCAs: caCertPool}
conf.BuildNameToCertificate()
tr := &http.Transport{TLSClientConfig: conf}
cli := &http.Client{Transport: tr}
resp, _ := cli.Head("https://www.google.com")
fmt.Println(resp.TLS.ServerName)
}
但它正陷入恐慌
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x88 pc=0x401314]
goroutine 1 [running]:
panic(0x7145e0, 0xc82000a100)
/usr/local/go/src/runtime/panic.go:464 +0x3e6
main.main()
/home/huangjian/anti-GFW/go-checkgoogleip/checkip.go:22 +0x314
exit status 2
也许我缺乏对公钥加密的扎实了解,但现在我只想让它发挥作用。有谁能告诉我我在哪里做错了以及如何纠正它?