import ( "io" "log" "os" ) func main() { file, err := os.OpenFile("myfile.txt", os.O_RDWR|os.O_CREATE, 0755) if err != nil { log.Fatal(err) } defer file.Close() data := []byte("Hello World!") _, err = file.Write(data) if err != nil { log.Fatal(err) } buf := make([]byte, 1024) n, err := file.Read(buf) if err != nil { log.Fatal(err) } log.Printf("Read %d bytes: %s", n, buf[:n]) }
import ( "io" "log" "net" ) func main() { conn, err := net.Dial("tcp", "localhost:8080") if err != nil { log.Fatal(err) } defer con.Close() data := []byte("Hello World!") _, err = conn.Write(data) if err != nil { log.Fatal(err) } buf := make([]byte, 1024) n, err := conn.Read(buf) if err != nil { log.Fatal(err) } log.Printf("Read %d bytes: %s", n, buf[:n]) }This code connects to a server at localhost:8080 using TCP and writes "Hello World!" to it. Then it reads the server response and logs it. The connection is then closed.