Advertisement
zeroidentidad

cliente.go

Nov 17th, 2020
1,363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.66 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "io"
  5.     "log"
  6.     "net"
  7.     "os"
  8. )
  9.  
  10. func main() {
  11.     conexion, err := net.Dial("tcp", "localhost:8000")
  12.     if err != nil {
  13.         log.Fatal(err)
  14.     }
  15.  
  16.     //variable que ignores errores
  17.     done := make(chan struct{})
  18.  
  19.     // funcion anonima entrada y salida paquete "io"
  20.     go func() {
  21.         io.Copy(os.Stdout, conexion)
  22.         log.Println("Finalizado")
  23.         // avisar a goroutine principal
  24.         done <- struct{}{}
  25.     }()
  26.  
  27.     mustCopy(conexion, os.Stdin)
  28.     conexion.Close()
  29.     // esperar que goroutine de conexion al servidor termine
  30.     <-done
  31.  
  32. }
  33.  
  34. func mustCopy(dst io.Writer, src io.Reader) {
  35.     if _, err := io.Copy(dst, src); err != nil {
  36.         log.Fatal(err)
  37.     }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement