package main import ( "fmt" "os" ) func main() { file, err := os.Create("output.txt") if err != nil { panic(err) } defer file.Close() _, err = fmt.Fprintln(file, "Hello, world!") if err != nil { panic(err) } }
package main import ( "fmt" "net" ) func main() { conn, err := net.Dial("tcp", "example.com:80") if err != nil { panic(err) } defer conn.Close() _, err = fmt.Fprintf(conn, "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") if err != nil { panic(err) } }In this example, we connect to a web server over TCP/IP using the net.Dial function, then send an HTTP GET request to it using the fmt.Fprintf function. We then close the network connection using the defer statement. The package used here is "net". In summary, using the Go io.Writer interface provides a simple and consistent way to write data to different types of streams or buffers. By implementing the io.Writer interface, other packages can easily accept data from your code and write it to their designated destinations.