https://s3-us-west-2.amazonaws.com/secure.notion-static.com/30acd5dd-5d7e-4a1e-97b7-fc6d3c6cbf06/Untitled.png

Chapter 2. The use of Channel Afterthoughts

  1. When we comments out the line. The for loop that is meant to use to catch the results from workers are not being waited

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/5989d886-02bf-4a83-b57a-060a74aec1be/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/770e39f0-028f-4a3b-8346-e6fc8fcdf122/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2f4781b6-d401-49c0-ae79-d0690b21dc85/Untitled.png

  1. Vice versa. when we use channel to supply the data in a for loop. the for loop will wait for the workers to put result into channel results, and start executing.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8a800677-53b1-4f76-9c0f-3ee3eb035f15/Untitled.png


: meant to initalize NEW variables. therefore namespace can't be the same as the previous var.

Ex.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d39878d3-a797-4202-9b0c-b42576da1c42/Untitled.png

Get rid of : to reassign new value to the same variable ( same *)


For {}. can put it into a while loop?


ProxyServer

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)
	}

}

Os/exec