title: How to Send/Receive Go Unix Socket JSON string using encoder/decoder
body: <p>We are building a trivial cache process which runs on every node. Each has multiple services up and running and tries to connect to this local cache process which receives the JSON string as an input, and sends the required details in JSON format.</p>
  
  <p>As of now, we have tried the below approach and it seems the local socket communication with encoder, decoder is giving empty results.</p>
  
  <p><strong>Server Code</strong></p>
  
  <pre><code>package main
  
  import (
      "encoding/json"
      "fmt"
      "net"
      "os"
  )
  
  var sockLocArg = "/tmp/.testsock"
  
  type sockOut struct {
      keyCheckSum   uint32 `json:"keyCheckSum"`
      valueCheckSum uint32 `json:"valueCheckSum"`
      emsg          string `json:"emsg"`
  }
  
  type sockIn struct {
      action   string `json:"action"`
      compName string `json:"compname"`
  }
  
  func processSockRequest(c net.Conn) {
  
      defer c.Close()
      decode := json.NewDecoder(c)
      encode := json.NewEncoder(c)
  
      var inputJSON sockIn
      err := decode.Decode(&amp;inputJSON)
      if err != nil {
          fmt.Printf("Error is %v", err)
      }
      fmt.Printf("Action: %s, CompName: %s\n", inputJSON.action, inputJSON.compName)
  
      outputJSON := sockOut{
          keyCheckSum:   10,
          valueCheckSum: 10,
          emsg:          "",
      }
      // Send response back to the socket request
      err = encode.Encode(outputJSON)
      if err != nil {
          fmt.Printf("Error is %v", err)
      }
  }
  
  func initSocket() {
      // Creating the unix domain TCP socket
      //
      localSocket, err := net.Listen("unix", sockLocArg)
      if err != nil {
          fmt.Printf("Unable to create unix domain socket. Error: %v", err)
          os.Exit(1)
      }
      // Set the permissions 700 on this
      //
      if err = os.Chmod(sockLocArg, 0700); err != nil {
          fmt.Printf("Unable to change the permissions for the socket. Error: %v", err)
          os.Exit(1)
      }
      // Initiate and listen to the socket
      //
      for {
          SockFileDescriptor, err := localSocket.Accept()
          if err != nil {
              fmt.Printf("Unable to accept incoming messages over the socket. Error: %v", err)
              os.Exit(1)
          }
          processSockRequest(SockFileDescriptor)
      }
  }
  
  func main() {
      initSocket()
  }
  </code></pre>
  
  <p><strong>Client Code</strong></p>
  
  <pre><code>package main
  
  import (
      "encoding/json"
      "fmt"
      "net"
  )
  
  type sockOut struct {
      keyCheckSum   uint32 `json:"keyCheckSum"`
      valueCheckSum uint32 `json:"valueCheckSum"`
      emsg          string `json:"emsg"`
  }
  
  type sockIn struct {
      action   string `json:"action"`
      compName string `json:"compname"`
  }
  
  func main() {
      c, err := net.Dial("unix", "/tmp/.testsock")
      if err != nil {
          panic(err)
      }
      defer c.Close()
  
      //go reader(c)
  
      decode := json.NewDecoder(c)
      encode := json.NewEncoder(c)
  
      inputJSON := sockIn{
          action:   "GET",
          compName: "TEST",
      }
      err = encode.Encode(inputJSON)
  
      if err != nil {
          fmt.Printf("Error is: %v", err)
      }
      var outputJSON sockOut
      err = decode.Decode(&amp;outputJSON)
  
      if err != nil {
          fmt.Printf("Error is %v", err)
      }
      fmt.Printf("KeyCheckSum: %d, ValueCheckSum: %d Error: %s\n", outputJSON.keyCheckSum, outputJSON.valueCheckSum, outputJSON.emsg)
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <p><em>Starting Server</em></p>
  
  <pre><code>bin/serv &amp;
  [1] 6195
  </code></pre>
  
  <p><em>Running Client</em></p>
  
  <pre><code>$ bin/client 
  Action: , CompName: 
  KeyCheckSum: 0, ValueCheckSum: 0 Error: 
  </code></pre>
  
  <p>Would someone provide some guidance on why we are getting this empty string between server and client communication. We are using Go version go1.8 darwin/amd64.</p>