package main import ( "fmt" "os" ) func main() { file, err := os.Create("test.txt") if err != nil { fmt.Println(err) return } defer file.Close() writer := file // Writing data to the file using WriteString method writer.WriteString("Hello World") // Flushing data to the file err = writer.Flush() if err != nil { fmt.Println(err) return } }
package main import ( "fmt" "net" ) func main() { conn, err := net.Dial("tcp", "google.com:80") if err != nil { fmt.Println(err) return } defer conn.Close() writer := conn // Writing data to the connection using Write method _, err = writer.Write([]byte("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")) if err != nil { fmt.Println(err) return } // Flushing data to the connection err = writer.Flush() if err != nil { fmt.Println(err) return } }In this example, we create a TCP connection to google.com on port 80. We create a writer for the connection using the net.Dial function. We then use the Write method to write "GET / HTTP/1.1\r\nHost: google.com\r\n\r\n" to the connection. Finally, we call the Flush method to flush any buffered data to the connection. Based on the code examples above, the package library used is "io".