%q
to quote a string in fmt.Sprintf
format specifiers GO-R400234
35// MarshalJSON returns the duration as a JSON string.
36func (d *Duration) MarshalJSON() ([]byte, error) {
37 return []byte(fmt.Sprintf(`"%s"`, d.String())), nil38}
39
40// UnmarshalJSON parses a JSON string into the duration.
%q
is same as "%s"
.
str := fmt.Sprintf("str: "%s"", "Hello World")
// str => str: "Hello World"
str := fmt.Sprintf("str: %q", "Hello World")
// str => str: "Hello World"