title: Accessing Golang struct that implements error interface causes function to never return
body: <h1>Background</h1>
  
  <p>I'm unmarshalling JSON data from an HTTP API to the following Golang structs:</p>
  
  <pre><code>type ResponseBody struct {
      Version string `json:"jsonrpc"`
      Result  Result `json:"result"`
      Error   Error  `json:"error"`
      Id      int    `json:"id"`
  }
  
  type Result struct {
      Random struct {
          Data           interface{} `json:"data"`
          CompletionTime string      `json:"completionTime"`
      } `json:"random"`
      BitsUsed      int `json:"bitsUsed"`
      BitsLeft      int `json:"bitsLeft"`
      RequestsLeft  int `json:"requestsLeft"`
      AdvisoryDelay int `json:"advisoryDelay"`
  }
  
  type Error struct {
      Code    int    `json:"code"`
      Message string `json:"message"`
      Data    []int  `json:"data,omitempty"`
  }
  </code></pre>
  
  <p>and I've implemented the error interface for <code>Error</code> as follows:</p>
  
  <pre><code>func (e Error) Error() string {
      return fmt.Sprintf("Error: %+v", e)
  }
  </code></pre>
  
  <p>Relevant code so far:</p>
  
  <pre><code>func Request(method string, params interface{}) (Result, error) {
      // `error` in return types is _not_ a typo
  
      body, err := json.Marshal(RequestBody{Version: "2.0", Params: params, Method: method, Id: 1})
      if err != nil {
          return Result{}, err
      }
  
      resp, err := http.Post(endpoint, "application/json-rpc", bytes.NewReader(body))
      if err != nil {
          return Result{}, fmt.Errorf("Request failed, error was %s", err)
      }
      defer resp.Body.Close()
  
      text, err := ioutil.ReadAll(resp.Body)
      if err != nil {
          return Result{}, fmt.Errorf("Failed to read response into memory, error was: %s", err)
      }
  
      if resp.StatusCode != 200 {
          var errorMessage Error
          if err := json.Unmarshal(text, &amp;errorMessage); err != nil {
              return Result{}, Error{
                  Code:    409,
                  Message: fmt.Sprintf("Client could not decode JSON error response, received %s, error was: %s", text, err),
                  Data:    []int{},
              }
          }
          return Result{}, errorMessage
      }
  
      response := ResponseBody{}
      if err := json.Unmarshal(text, &amp;response); err != nil {
          return Result{}, fmt.Errorf("Failed to JSON-decode response body, received %s from source, error was: %s", text, err)
      }
  
      return response.Result, response.Error
  }
  </code></pre>
  
  <h1>Issue</h1>
  
  <p>The following code hangs indefinitely on a successful call without panicking:</p>
  
  <pre><code>// `body` here is just any old struct
  result, err := Request("generateIntegers", body)
  if err != nil {
      return []int{}, err  // hangs here
  }
  </code></pre>
  
  <p>In fact, the code always hangs when I invoke <code>err</code>. No panic is raised and no error is returned - it is simply frozen[1].</p>
  
  <p>[1] Strictly speaking, it causes a stack overflow error, but that's because the function never returns and so the deferred <code>resp.Body.Close()</code> in <code>Request</code> never gets called. </p>
  
  <p>It gets weirder. Adding the following debugging lines:</p>
  
  <pre><code>response := ResponseBody{}
  if err := json.Unmarshal(text, &amp;response); err != nil {
      return Result{}, fmt.Errorf("Failed to JSON-decode response body, received %s from source, error was: %s", text, err)
  }
  
  fmt.Println(response.Result.BitsUsed)
  fmt.Println(response.Result) // this prints!
  
  return response.Result, response.Error
  </code></pre>
  
  <p>works, but changing these lines to just</p>
  
  <pre><code>response := ResponseBody{}
  if err := json.Unmarshal(text, &amp;response); err != nil {
      return Result{}, fmt.Errorf("Failed to JSON-decode response body, received %s from source, error was: %s", text, err)
  }
  
  fmt.Println(response.Result) // this no longer prints! :O
  
  return response.Result, response.Error
  </code></pre>
  
  <p>causes <code>Request</code> function itself to hang at this debug statement.</p>
  
  <h1>Things I've Tried</h1>
  
  <ul>
  <li>Running a trace with <code>go test -trace</code> to view what's being called. This fails because <code>go tool trace</code> cannot parse the generated trace file.</li>
  <li>Converting my signature to return <code>*error</code> instead of regular <code>error</code>. This did not help.</li>
  </ul>
  
  <hr>
  
  <p>Why is this code snippet hanging?</p>
  
  <p>Note: I am running Go 1.7</p>