title: Making Golang TCP server concurrent
body: <p>New to Go and trying to make a TCP server concurrent. I found multiple examples of this, including <a href="https://codereview.stackexchange.com/questions/181415/golang-concurrent-tcp-server">this one</a>, but what I am trying to figure out is why some changes I made to a non concurrent version are not working.</p>
  
  <p>This is the original sample code I started from</p>
  
  <pre><code>package main
  import "bufio"
  import "fmt"
  import "log"
  import "net"
  import "strings" // only needed below for sample processing
  
  func main() {
    fmt.Println("Launching server...")
    fmt.Println("Listen on port")
    ln, err := net.Listen("tcp", "127.0.0.1:8081")
    if err != nil {
        log.Fatal(err)
    }
    defer ln.Close()
  
    fmt.Println("Accept connection on port")
    conn, err := ln.Accept()
    if err != nil {
        log.Fatal(err)
    }
  
    fmt.Println("Entering loop")
    // run loop forever (or until ctrl-c)
    for {
      // will listen for message to process ending in newline (\n)
      message, _ := bufio.NewReader(conn).ReadString('\n')
      // output message received
      fmt.Print("Message Received:", string(message))
      // sample process for string received
      newmessage := strings.ToUpper(message)
      // send new string back to client
      conn.Write([]byte(newmessage + "\n"))
    }
  }
  </code></pre>
  
  <p>The above works, but it is not concurrent.</p>
  
  <p>This is the code after I modified it</p>
  
  <pre><code>package main
  import "bufio"
  import "fmt"
  import "log"
  import "net"
  import "strings" // only needed below for sample processing
  
  func handleConnection(conn net.Conn) {
    fmt.Println("Inside function")
    // run loop forever (or until ctrl-c)
    for {
      fmt.Println("Inside loop")
      // will listen for message to process ending in newline (\n)
      message, _ := bufio.NewReader(conn).ReadString('\n')
      // output message received
      fmt.Print("Message Received:", string(message))
      // sample process for string received
      newmessage := strings.ToUpper(message)
      // send new string back to client
      conn.Write([]byte(newmessage + "\n"))
    }
  
  }
  
  func main() {
    fmt.Println("Launching server...")
    fmt.Println("Listen on port")
    ln, err := net.Listen("tcp", "127.0.0.1:8081")
    if err != nil {
        log.Fatal(err)
    }
    //defer ln.Close()
  
    fmt.Println("Accept connection on port")
    conn, err := ln.Accept()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Calling handleConnection")
    go handleConnection(conn)
  
  }
  </code></pre>
  
  <p>I based my code on several other examples I found of concurrent servers, but yet when I run the above the server seems to exit instead of running the handleConnection function</p>
  
  <blockquote>
    <p>Launching server...</p>
    
    <p>Listen on port </p>
    
    <p>Accept connection on port </p>
    
    <p>Calling handleConnection</p>
  </blockquote>
  
  <p>Would appreciate any feedback as similar code examples I found and tested using the same approach, concurrently calling function to handle connections, worked; so, would like to know what is different with my modified code from the other samples I saw since they seem to be the same to me.</p>
  
  <p>I was not sure if it was the issue, but I tried commenting the defer call to close. That did not help.</p>
  
  <p>Thanks.</p>