I am building a https client in golang, which is supposed to connect to a specified host and get the server name from the header or the certificate of the host. This is my code
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)
}
But it is running into a panic
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
Maybe I am lack of solid knowledge of public key encryption but now I just want to make it work. Could anyone tell me where did I do wrongly and how to correct it?