title: How do you escape characters within a string (JSON format)
body: <p>What is the idiomatic way to escape only the characters in a string require to be escaped by the  <a href="https://www.json.org" rel="noreferrer">JSON specification</a>.</p>
  
  <p>(I am not trying to marshal/unmarshal an object or string, I just want to escape the characters inside a string. </p>
  
  <p>This works, but surely there is a more idiomatic way? <a href="https://play.golang.org/p/rcHZbrjFyyH" rel="noreferrer">https://play.golang.org/p/rcHZbrjFyyH</a></p>
  
  <pre><code>func main() {
      fmt.Println(jsonEscape(`dog "fish" cat`))
      //output: dog \"fish\" cat
  }
  
  func jsonEscape(i string) string {
      b, err := json.Marshal(i)
      if err != nil {
          panic(err)
      }
      // Trim the beginning and trailing " character
      return string(b[1:len(b)-1])
  }
  </code></pre>