gofmt from stdin

355 views
Skip to first unread message

bsr

unread,
Sep 27, 2014, 9:36:01 PM9/27/14
to golan...@googlegroups.com
Hello,

What is the best way to gofmt a program which is read from stdin. I am not too familiar with piping, but this almost work


echo "package foo; type T struct {
Var1    type1   `json:"var,omitempty"` // somecomment1 
}" | gofmt

this outputs

-bash: json:var,omitempty: command not found

package foo


type T struct {

Var1 type1 // somecomment1

}

how can I make it to use field tag? or is there a better way than echoing the string.

For the context, I am calling this through node.js

function format(src) {
            var cmd = 'echo "' + src + '" | gofmt'
            child_process.exec(cmd, function (err, stdout, stderr) { 
            ....
           });
}

where src is the program.

Wael M. Nasreddine

unread,
Sep 27, 2014, 9:47:26 PM9/27/14
to bsr, golan...@googlegroups.com
On Sat, Sep 27, 2014 at 6:36 PM, bsr <bsr...@gmail.com> wrote:
Hello,

What is the best way to gofmt a program which is read from stdin. I am not too familiar with piping, but this almost work


echo "package foo; type T struct {
Var1    type1   `json:"var,omitempty"` // somecomment1 
}" | gofmt


The backtick in here (the one right before the word json) is being evaluated as a subshell, so you need to wrap everything in a single quote rather than a double quote, which also means you need to escape inner double quotes with a backslash.
 
this outputs

-bash: json:var,omitempty: command not found

package foo


type T struct {

Var1 type1 // somecomment1

}

how can I make it to use field tag? or is there a better way than echoing the string.

For the context, I am calling this through node.js

function format(src) {
            var cmd = 'echo "' + src + '" | gofmt'
            child_process.exec(cmd, function (err, stdout, stderr) { 
            ....
           });
}

where src is the program.

--
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.
For more options, visit https://groups.google.com/d/optout.



--
Wael Nasreddine | SRE at Google | wael.na...@gmail.com | (650) 735-1773

bsr

unread,
Sep 27, 2014, 10:02:18 PM9/27/14
to golan...@googlegroups.com, bsr...@gmail.com
thank you. it worked, I only changed this line 

    var cmd = "echo '" + d + "' | gofmt";

I didn't have to escape the double quotes (if I do, the backslash is in the output).

thanks again for your help.

hope echoing like this is still the best way to read from stdin.

luz...@gmail.com

unread,
Sep 28, 2014, 4:50:37 AM9/28/14
to golan...@googlegroups.com, bsr...@gmail.com
On Sunday, September 28, 2014 4:02:18 AM UTC+2, bsr wrote:
hope echoing like this is still the best way to read from stdin.

No, it's not the best way to write to stdin from Node.js. Try this:

var exec = require('child_process').exec;

var src = 'package main\nimport "fmt"\nfunc main() {\nfmt.Println("hello, world")\n}';

var gofmt = exec('gofmt', function (err, stdout, stderr) {
  console
.log(stdout);
});
gofmt
.stdin.setEncoding = 'utf-8';
gofmt
.stdin.write(src);
gofmt
.stdin.end();



bsr

unread,
Sep 28, 2014, 8:24:51 AM9/28/14
to golan...@googlegroups.com, bsr...@gmail.com, luz...@gmail.com
looks much better .. thank you. 

On a side note, it works, but "setEncoding" is for readable stream (process.stdin). but as per the manual, child_process.stdin, writable stream, don't have it. 

thanks again for your help.

Reply all
Reply to author
Forward
0 new messages