I have a string

{"function":"TrainingQuestionInfo","values":{"Question":"текст со спецсимволами / \ и т.д."}} 

I need to escape quotes and slashes.
How to do it? Is there any function for this?

  • The line usually needs to be escaped before transferring somewhere, where will you send it? - rekby
  • figured out - this option fits golang.org/pkg/strconv/#Quote - Rakzin Roman

4 answers 4

https://golang.org/pkg/strconv/#Quote

fmt.Println (strconv.Quote ( {"function":"TrainingQuestionInfo","values":{"Question":"текст со спецсимволами / \ и т.д."}} ))

    Escaping usually depends on where this string is sent.

    For example, you can use JSON:

     b, err := json.Marshal("aaa\nbbbыыы") fmt.Printf("str=`%v`, err=%v\n", string(b), err) // выводит str=`"aaa\nbbbыыы"`, err=<nil> 

    >>> playground code <<<

      \\ for a slash, \" for quotation marks. Everything is as it is ... PS. Automation of the solution - first replacing all the slashes, then - quotes.

      • non-i wanted some function. what would she do it - Rakzin Roman
      • So write in the question that you need a function for screening. True, I do not quite understand why this is ... - Vladimir Martyanov
      • The line usually needs to be escaped before transferring somewhere, where will you send it? - rekby
      • @rekby depends on where it should be transferred and how it already exists ... - Vladimir Martyanov
      • In the go programming context, a string can seem to be only in one form: inside a variable of type string, everything else is not a string anymore, but something else. - rekby

      From String Literals ( http://golang.org/ref/spec#String_literals ):

      • raw string literal supports multi-line (but escapes are not interpreted)
      • an interpreted string literal interprets escapes, such as '\ n'.

      But if your multi-line string must include backquote (`), then you will have to use an interpreted string literal:

        `line one line two ` + "`" + `line three line four` 

      You cannot directly put backquote ( ) в строковый литерал строки ( xx). You should use (as explained in how to put a return quote in the string to return to its original position "):

      • "` "+ ...