title: Golang TCPConn Gob Communication
body: <p>I'm having issues with the gob protocol (or maybe networking in general, where my knowledge is weak), and I don't understand why the following code does not work properly. It is just supposed to be a simple example of maintaining an open TCP connection, and sending multiple gobs through it. The code will send, and receive, but often corrupts its data. 
  Thank you in advance.</p>
  
  <pre><code>package main
  
  import (
      "encoding/gob"
      "fmt"
      "net"
      "strconv"
      "time"
  )
  
  type Message struct {
      Msg string
  }
  
  func main() {
      gob.Register(new(Message))
  
      clientAddr, err := net.ResolveTCPAddr("tcp", "localhost:12346")
      if err != nil {
          fmt.Println(err)
      }
      serverAddr, err := net.ResolveTCPAddr("tcp", "localhost:12345")
      if err != nil {
          fmt.Println(err)
      }
  
      serverListener, err := net.ListenTCP("tcp", serverAddr)
      if err != nil {
          fmt.Println(err)
      }
      conn, err := net.DialTCP("tcp", clientAddr, serverAddr)
      if err != nil {
          fmt.Println(err)
      }
      serverConn, err := serverListener.AcceptTCP()
      if err != nil {
          fmt.Println(err)
      }
      done := false
      go func() {
          for !done {
              recieveMessage(serverConn)
          }
      }()
      for i := 1; i &lt; 1000; i++ {
          sent := Message{strconv.Itoa(i)}
          sendMessage(sent, conn)
      }
      time.Sleep(time.Second)
      done = true
  }
  
  func sendMessage(msg Message, conn *net.TCPConn) {
      enc := gob.NewEncoder(conn)
      err := enc.Encode(msg)
      if err != nil {
          fmt.Println(err)
      }
  }
  
  func recieveMessage(conn *net.TCPConn) {
      msg := new(Message)
      dec := gob.NewDecoder(conn) // Will read from network.
      err := dec.Decode(msg)
      if err != nil {
          fmt.Println(err)
      }
      fmt.Println("Client recieved:", msg.Msg)
  }
  </code></pre>