use gofmt to replace single quote with double quote

651 views
Skip to first unread message

6po...@gmail.com

unread,
Mar 3, 2016, 5:10:05 PM3/3/16
to golang-nuts
Hi! I'd like to do just that. No tr(I'm using Linux).  I understand that it must be a valid go expression. Nonetheless, why can't I just replace  single quote with double quote without using tr or other tools? Please, improve gofmt or ar least enlighten me.

Ian Lance Taylor

unread,
Mar 3, 2016, 5:21:19 PM3/3/16
to 6po...@gmail.com, golang-nuts
What exactly have you tried?

Ian

6po...@gmail.com

unread,
Mar 3, 2016, 5:27:51 PM3/3/16
to golang-nuts, 6po...@gmail.com
 gofmt -r "'->\"" book.go
 parsing pattern ' at 1:1: rune literal not terminated

And, please, show me how to do it. I don't wanna use any double quotes at all. Only single quotes.

Tapio Raevaara

unread,
Mar 3, 2016, 5:57:17 PM3/3/16
to golang-nuts, 6po...@gmail.com
On Friday, March 4, 2016 at 12:10:05 AM UTC+2, 6po...@gmail.com wrote:
Hi! I'd like to do just that. No tr(I'm using Linux).  I understand that it must be a valid go expression. Nonetheless, why can't I just replace  single quote with double quote without using tr or other tools? Please, improve gofmt or ar least enlighten me.

Uh... why should gofmt be changed for your specific needs simply because your *refuse* (for whatever reason) to write correct Go code in the first place? There are numerous other tools that can already do this for you, including your editor's search/replace functionality. And what's wrong with using tr?

Ian Lance Taylor

unread,
Mar 3, 2016, 6:11:29 PM3/3/16
to 6po...@gmail.com, golang-nuts
On Thu, Mar 3, 2016 at 2:27 PM, <6po...@gmail.com> wrote:
> gofmt -r "'->\"" book.go
> parsing pattern ' at 1:1: rune literal not terminated
>
>
> And, please, show me how to do it. I don't wanna use any double quotes at
> all. Only single quotes.

You can't do this using gofmt. It doesn't make sense; the gofmt tool
is specific to the Go language, and in Go single quotes and double
quotes mean different things.

Ian

Kerem Gocen

unread,
Mar 3, 2016, 6:49:40 PM3/3/16
to golang-nuts
I believe mister, what you need is just some regex love and a sane text editor with regex find/replace.

The first result of a google search with the last part of your question is here;http://stackoverflow.com/a/17009339/1505341

Good luck!

andrewc...@gmail.com

unread,
Mar 3, 2016, 7:19:35 PM3/3/16
to golang-nuts, 6po...@gmail.com


On Friday, March 4, 2016 at 11:10:05 AM UTC+13, 6po...@gmail.com wrote:
Hi! I'd like to do just that. No tr(I'm using Linux).  I understand that it must be a valid go expression. Nonetheless, why can't I just replace  single quote with double quote without using tr or other tools? Please, improve gofmt or ar least enlighten me.

Your tone is as if people on the internet owe you something. Maybe that isn't your intent, but it rub people the wrong way. 

6po...@gmail.com

unread,
Mar 4, 2016, 1:01:16 AM3/4/16
to golang-nuts, 6po...@gmail.com
Guys, hi again! You see, a simple "You can't do it using gofmt."  is enough. I already mentioned in my very first post that I can (and know!) how to replace single quotes using other tools like tr. I just felt (and still feel) that using gofmt to replace characters in .go files is... natural. Or maybe normal? Don't know.


On Friday, March 4, 2016 at 12:10:05 AM UTC+2, 6po...@gmail.com wrote:

Konstantin Khomoutov

unread,
Mar 4, 2016, 7:40:42 AM3/4/16
to 6po...@gmail.com, golang-nuts
On Thu, 3 Mar 2016 22:00:52 -0800 (PST)
6po...@gmail.com wrote:

> I just felt (and still feel) that using gofmt to replace characters
> in .go files is... natural. Or maybe normal? Don't know.

No, it's not natural for a simple reason: `gofmt` is a tool which
works not really on the text level (like `tr` does) but on a level
above it--Go's syntax tree. To explain it in simple words, `gofmt`
works like this: it takes a file presumably containing Go code, parses
it into the so-called "abstract syntax tree" (AST) then serializes that
AST back into text--using the formatting rules declared to be
"reference". After it had parsed a file into an AST, it's able to
apply certain transformation to that AST before serializing it back to
text, and that's what you specify by the "-r" command-line option to
`gofmt`.

Naturally, due to the way it works, `gofmt` expects two things:
1) What it reads and parses is a valid Go code.
2) The transformation supplied to it should be a valid Go code on both
sides.

You can now see, that making `gofmt` apply a mere text transformation
to a piece of text which is known do be an incorrect Go code is futile
by all means.

6po...@gmail.com

unread,
Mar 4, 2016, 8:02:18 AM3/4/16
to golang-nuts, 6po...@gmail.com
Hi! Again! `gofmt -w book.go` inserts spaces in my book.go, but it can't and it won't replace single quotes with double quotes. Why? Because it's just not normal.


On Friday, March 4, 2016 at 12:10:05 AM UTC+2, 6po...@gmail.com wrote:

Jan Mercl

unread,
Mar 4, 2016, 8:11:34 AM3/4/16
to 6po...@gmail.com, golang-nuts

On Fri, Mar 4, 2016 at 2:02 PM <6po...@gmail.com> wrote:

> Hi! Again! `gofmt -w book.go` inserts spaces in my book.go, but it can't and it won't replace single quotes with double quotes. Why? Because it's just not normal.

First of all, gofmt refuses to format syntactically invalid code.

If the code is syntactically valid, replacing single quotes in it with double quotes would make it invalid in almost all cases*. That's definitely not what gofmt is intended for and also it's probably why it's not what you want.

TBH, I don't understand what's your goal.

(*): var v interface{} = 'x' // Eg. this would still work after rewriting.

--

-j

Tamás Gulácsi

unread,
Mar 4, 2016, 8:16:45 AM3/4/16
to golang-nuts, 6po...@gmail.com
2016. március 4., péntek 14:02:18 UTC+1 időpontban 6po...@gmail.com a következőt írta:
Hi! Again! `gofmt -w book.go` inserts spaces in my book.go, but it can't and it won't replace single quotes with double quotes. Why? Because it's just not normal.

No, it's because gofmt _formats_ your _go_ source code to the canonical format.

As an extra feature, it also allows you to specify some replacemenets, but both of the replacement and the replacer must be valid go code.
This is not the basic functionality of gofmt, just a nice addition.
So please don't lament about why can't it do as tr does - it's not his duty!

6po...@gmail.com

unread,
Mar 4, 2016, 8:18:01 AM3/4/16
to golang-nuts, 6po...@gmail.com
Sorry. I thought Go and Go tools are evolving. I thought that maybe, one day, my posts will get to shape Go. And the Go tools.


On Friday, March 4, 2016 at 12:10:05 AM UTC+2, 6po...@gmail.com wrote:

Tapio Raevaara

unread,
Mar 4, 2016, 11:45:43 AM3/4/16
to golang-nuts, 6po...@gmail.com

On Friday, March 4, 2016 at 3:18:01 PM UTC+2, 6po...@gmail.com wrote:
Sorry. I thought Go and Go tools are evolving. I thought that maybe, one day, my posts will get to shape Go. And the Go tools.

They *are* evolving, but evolution doesn't mean what you think it means. Evolution is adaptation to better suit one's niche. Arbitrary text transformations are not gofmt's niche.

The Unix philosophy is to write programs that do one thing and do it well - and Unix and Go, unsurprisingly, share a lot of philosophy. Gofmt does code formatting, and it does it well. Other tools, such as sed and tr, do text transformations, and they do it well.

Use the right tool for the job, instead of insisting that your hammer should be updated so that it could be used as a screwdriver. It wouldn't be an improvement.
Reply all
Reply to author
Forward
0 new messages