file, err := os.Create("output.txt") if err != nil { log.Fatal(err) } defer file.Close() data := []byte("Hello, world!") n, err := file.Write(data) if err != nil { log.Fatal(err) } fmt.Printf("Wrote %d bytes to file\n", n)
data := []byte("Hello, world!") n, err := os.Stdout.Write(data) if err != nil { log.Fatal(err) } fmt.Printf("Wrote %d bytes to stdout\n", n)
conn, err := net.Dial("tcp", "example.com:80") if err != nil { log.Fatal(err) } defer conn.Close() data := []byte("GET / HTTP/1.0\r\n\r\n") n, err := conn.Write(data) if err != nil { log.Fatal(err) } fmt.Printf("Wrote %d bytes to network\n", n)In the above example, we connect to a web server at example.com on port 80, write an HTTP request to it, and close the connection. We use the Write method of the network connection object to write bytes to the socket. This method returns the number of bytes written and an error if any. Package library: "net"