What is the idiomatic way to escape only the characters in a string require to be escaped by the JSON specification.
(I am not trying to marshal/unmarshal an object or string, I just want to escape the characters inside a string.
This works, but surely there is a more idiomatic way? https://play.golang.org/p/rcHZbrjFyyH
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])
}