package main import ( "log" "net" "net/textproto" ) func main() { conn, err := net.Dial("tcp", "127.0.0.1:8080") if err != nil { log.Fatalf("Dial error: %v", err) } defer conn.Close() tp := textproto.NewConn(conn) if _, err := tp.PrintfLine("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", "example.com"); err != nil { log.Fatalf("PrintfLine error: %v", err) } }
package main import ( "fmt" "net" "net/textproto" ) func main() { conn, err := net.Dial("tcp", "smtp.example.com:25") if err != nil { panic(err) } defer conn.Close() tp := textproto.NewConn(conn) if _, err := tp.ReadResponse(220); err != nil { panic(err) } if _, err := tp.PrintfLine("EHLO example.com"); err != nil { panic(err) } if _, err := tp.ReadResponse(250); err != nil { panic(err) } fmt.Println("Connection Established") }In this example, we use Conn.PrintfLine to send an EHLO command to a SMTP server. The `tp.ReadResponse` function is used to read the response code from the server and ensure that the command was successful. If an error is encountered, the program will panic. This example demonstrates the use of the net.textproto package to interact with a network protocol and handle errors using panic.