Function to escape and unscape string

191 views
Skip to first unread message

nadashin

unread,
Aug 29, 2021, 3:02:42 PM8/29/21
to golan...@googlegroups.com
fmt.Printf has a format specifier, %q that escapes string with Go
syntax and add quotes around the string. ( %#v also does it)

But it doesn't have one that unescapes a string.

I couldn't find any stdlib function that escape and unescape a string
following Go syntax. (and doesn't add quotes around the string)

nadashin

unread,
Aug 29, 2021, 3:14:57 PM8/29/21
to golan...@googlegroups.com
> I couldn't find any stdlib function that escape and unescape a string
> following Go syntax. (and doesn't add quotes around the string)

If the stdlib doesn't have such functions I am proposing to add them.

Jan Mercl

unread,
Aug 29, 2021, 3:18:24 PM8/29/21
to nadashin, golan...@googlegroups.com
Maybe https://pkg.go.dev/strconv#Unquote is what you want.

nadashin

unread,
Aug 29, 2021, 3:53:02 PM8/29/21
to Jan Mercl, golan...@googlegroups.com
> > fmt.Printf has a format specifier, %q that escapes string with Go
> > syntax and add quotes around the string. ( %#v also does it)
> >
> > But it doesn't have one that unescapes a string.
> >
> > I couldn't find any stdlib function that escape and unescape a string
> > following Go syntax. (and doesn't add quotes around the string)
>
> Maybe https://pkg.go.dev/strconv#Unquote is what you want.

No, that's not what I am looking for.

jake...@gmail.com

unread,
Aug 29, 2021, 6:39:45 PM8/29/21
to golang-nuts
https://pkg.go.dev/strconv#Unquote seems to be what you are describing. Perhaps you could explain how it fails to do what you want. Maybe a playground example?

Christopher Puschmann

unread,
Aug 29, 2021, 10:02:12 PM8/29/21
to nadashin, Jan Mercl, golan...@googlegroups.com
Hi,

have you tried fmt.Sscanf? https://play.golang.org/p/Ebr9VcobOcE
-- 
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/u9PmcdYmEIsTinPPzTNK6e3I1ANlGsvftmP4pKCptQj1Gupoz7wVr2F1BxcrApcL0Anoh-to1xJEkp4z6Y7RRtuNzBgAPEQFRnuYp4JCfSM%3D%40protonmail.com.

tapi...@gmail.com

unread,
Sep 5, 2021, 12:56:07 AM9/5/21
to golang-nuts
I looks the root cause is reflect.TypeOf and ValueOf make the values
referenced by the arguments escape, though often this is over-cautious.

jake...@gmail.com

unread,
Sep 5, 2021, 9:39:30 AM9/5/21
to golang-nuts
On Sunday, September 5, 2021 at 12:56:07 AM UTC-4 tapi...@gmail.com wrote:
I looks the root cause is reflect.TypeOf and ValueOf make the values
referenced by the arguments escape, though often this is over-cautious.

I think you misunderstood the problem. The question  has to do with character escaping in strings, not memory escaping to the heap.

Of course we don't actually know what the problem the OP is having with escaping strings, because the original post is vague. 
Reply all
Reply to author
Forward
0 new messages