When we comments out the line. The for loop that is meant to use to catch the results
from workers
are not being waited
workers
to put result
into channel results
, and start executing.:
meant to initalize NEW variables. therefore namespace can't be the same as the previous var.Ex.
Get rid of :
to reassign new value to the same variable ( same *
)
For {}
. can put it into a while loop?package main
import (
"io"
"log"
"net"
)
func handler(src net.Conn) {
// the site we want to port forward to
dst, err := net.Dial("tcp", "127.0.0.1:8080")
if err != nil {
log.Fatalln("unable to connect to remote server")
}
defer dst.Close()
go func() {
_, err := io.Copy(dst, src)
if err != nil {
log.Fatalln("Unable to copy from the client")
}
}()
// read from dst and put it back to the source
if _, err := io.Copy(src, dst); err != nil {
log.Fatalln("Unable to send to the oringal client")
}
}
func main() {
listener, err := net.Listen("tcp", ":9002")
if err != nil {
log.Fatalln("Unable to bind port at 9002")
}
for {
conn, err := listener.Accept()
if err != nil {
log.Fatalln("Unable to accept connection")
}
go handler(conn)
}
}