strings: escape sequence translation

83 views
Skip to first unread message

nadashin

unread,
Sep 6, 2021, 10:40:15 PM9/6/21
to golan...@googlegroups.com
Why is the function in Go compiler that interprets escape sequences not
available in Go's standard library?

Kurtis Rader

unread,
Sep 6, 2021, 10:56:07 PM9/6/21
to nadashin, golan...@googlegroups.com
On Mon, Sep 6, 2021 at 7:40 PM 'nadashin' via golang-nuts <golan...@googlegroups.com> wrote:
Why is the function in Go compiler that interprets escape sequences not
available in Go's standard library?

It does. See https://pkg.go.dev/strconv#Unquote. Try this hastily thrown together example:

package main

import (
"fmt"
"strconv"
)

func main() {
e := strconv.Quote("a\033\r\nb")
v, err := strconv.Unquote(e)
fmt.Printf("%v .%v. .%v.\n", err, e, v)
}
 

--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

nadashin

unread,
Sep 7, 2021, 8:30:11 AM9/7/21
to Kurtis Rader, golan...@googlegroups.com
On Tuesday, September 7th, 2021 at 08:55, Kurtis Rader <kra...@skepticism.us> wrote:

> It does. See https://pkg.go.dev/strconv#Unquote. Try this hastily thrown together example:
>
> package main
>
> import (
> "fmt"
> "strconv"
> )
>
> func main() {
> e := strconv.Quote("a\033\r\nb")
> v, err := strconv.Unquote(e)
> fmt.Printf("%v .%v. .%v.\n", err, e, v)
> }

This is silly. I want to be able to pass any valid string and get a result.

Jan Mercl

unread,
Sep 7, 2021, 8:46:07 AM9/7/21
to nadashin, Kurtis Rader, golan...@googlegroups.com
On Tue, Sep 7, 2021 at 2:29 PM 'nadashin' via golang-nuts
<golan...@googlegroups.com> wrote:

> > It does. See https://pkg.go.dev/strconv#Unquote. Try this hastily thrown together example:
> >
> > package main
> >
> > import (
> > "fmt"
> > "strconv"
> > )
> >
> > func main() {
> > e := strconv.Quote("a\033\r\nb")
> > v, err := strconv.Unquote(e)
> > fmt.Printf("%v .%v. .%v.\n", err, e, v)
> > }
>
> This is silly. I want to be able to pass any valid string and get a result.

That's exactly what the code demonstrates. Please provide an example
of some code you need to make work and where do you get stuck. Or some
other clue enabling others to help you. Thanks.

Jan Mercl

unread,
Sep 7, 2021, 10:07:05 AM9/7/21
to nadashin, golan...@googlegroups.com
FTR: This seems to be an unfortunate way of continuing the thread at
https://groups.google.com/g/golang-nuts/c/kBj-yEJuMtM/m/LKdFK_cZHAAJ
without ever answering the questions that others asked there while
trying to help you.

nadashin

unread,
Sep 7, 2021, 3:20:23 PM9/7/21
to golan...@googlegroups.com
Apologies for my lack of proper description, wrong assumptions and improper
understanding.

strconv.Unquote should be what I was looking for.

Only inconvenience is that other " in the string needs to be escaped, well
that's part of Go's syntax, so I should not complain.
Reply all
Reply to author
Forward
0 new messages