Advertisement
cydside

Escaping string JSON

Apr 23rd, 2023 (edited)
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.30 KB | None | 0 0
  1. // https://go.dev/play/p/khG7qBROaIx
  2.  
  3. package main
  4.  
  5. import (
  6.     "encoding/json"
  7.     "fmt"
  8. )
  9.  
  10. func main() {
  11.     fmt.Println(jsonEscape(`dog "fish" cat`))
  12. }
  13.  
  14. func jsonEscape(i string) string {
  15.     b, err := json.Marshal(i)
  16.     if err != nil {
  17.         panic(err)
  18.     }
  19.     s := string(b)
  20.     return s[1 : len(s)-1]
  21. }
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement