package main import ( "io" "log" "github.com/tarm/serial" ) func main() { // Open serial port port, err := serial.OpenPort(&serial.Config{Name: "/dev/ttyUSB0", Baud: 115200}) if err != nil { log.Fatal(err) } defer port.Close() // Write data to serial port _, err = port.Write([]byte("Hello world\n")) if err != nil { log.Fatal(err) } // Read data from serial port buf := make([]byte, 128) n, err := port.Read(buf) if err != nil { log.Fatal(err) } log.Printf("Received: %q", buf[:n]) }
package main import ( "io" "log" "net" ) func main() { // Dial a remote server conn, err := net.Dial("tcp", "golang.org:80") if err != nil { log.Fatal(err) } defer conn.Close() // Send HTTP request to server req := "GET / HTTP/1.1\r\nHost: golang.org\r\n\r\n" _, err = io.WriteString(conn, req) if err != nil { log.Fatal(err) } // Read HTTP response from server buf := make([]byte, 128) n, err := conn.Read(buf) if err != nil { log.Fatal(err) } log.Printf("Received: %q", buf[:n]) }In this example, we dial a remote server and send an HTTP request to it. We then read and print the HTTP response from the server using the ReadWriteCloser and Close methods. Both of the above examples use the "io" package and its ReadWriteCloser interface to work with data streams. The Close method is used to clean up any resources used by the stream when we are done with it.