Answer the question
In order to leave comments, you need to log in
How to escape string to insert into JSON string value?
I don’t work with Go so often, I can’t understand how can I use the built-in features of the language, without regular expressions, to convert a string so that it can be safely inserted as a JSON string? I can't google a function that escapes quotes and that's it.
You need it to implement a custom GQL scalar type:
// MarshalGQL implements the graphql.Marshaler interface
func (u Username) MarshalGQL(w io.Writer) {
_, err := w.Write([]byte(fmt.Sprintf(`"%s"`, u)))
if err != nil {
return
}
}
Answer the question
In order to leave comments, you need to log in
https://golang.cafe/blog/golang-json-marshal-examp...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question