// Create a TLS configuration with InsecureSkipVerify set to true. config := &tls.Config{InsecureSkipVerify: true} // Connect to a remote server using the specified configuration. conn, err := tls.Dial("tcp", "example.com:443", config) if err != nil { fmt.Println("Error connecting:", err) return } defer conn.Close() // Send a request to the server and read the response. _, err = conn.Write([]byte("GET / HTTP/1.0\r\n\r\n")) if err != nil { fmt.Println("Error sending request:", err) return } resp, err := ioutil.ReadAll(conn) if err != nil { fmt.Println("Error reading response:", err) return } fmt.Println(string(resp))
// Load a CA certificate from file caCert, err := ioutil.ReadFile("ca.pem") if err != nil { fmt.Println("Error reading CA certificate:", err) return } // Create a TLS configuration with the CA certificate and InsecureSkipVerify set to false. config := &tls.Config{ RootCAs: x509.NewCertPool(), InsecureSkipVerify: false, } config.RootCAs.AppendCertsFromPEM(caCert) // Connect to a remote server using the specified configuration. conn, err := tls.Dial("tcp", "example.com:443", config) if err != nil { fmt.Println("Error connecting:", err) return } defer conn.Close() // Send a request to the server and read the response. _, err = conn.Write([]byte("GET / HTTP/1.0\r\n\r\n")) if err != nil { fmt.Println("Error sending request:", err) return } resp, err := ioutil.ReadAll(conn) if err != nil { fmt.Println("Error reading response:", err) return } fmt.Println(string(resp))This example loads a CA (Certificate Authority) certificate from a file and creates a TLS configuration that includes the CA certificate. InsecureSkipVerify is set to false, which means that the server certificate will be verified against the CA certificate. This is a more secure way of creating a TLS connection than skipping verification altogether. This example should be used in production environments where security is a concern. The crypto.tls package is part of the Go standard library.