Suppose we need to run the following command in Go:
PS -A | grep wget
There are two things to be written here, as follows, the first command is
cmd
, the second one iscmd2
:
cmd := ("PS", "-A") cmd2 := ("grep", "wget")
Then use a pipeline to connect the standard output and standard input of both. Pay attention to the first commandcmd
The standard output should be used()
, notStdout
, as follows (Errors are ignorederr
and its handling):
, _ = ()
becauseIt's one
, is a writer, because this output is to be written to some places. And the same is true,
It's one
, is a reader used to read content in some places. If the two are assigned directly, the type mismatch error will occur. So need to use
StdoutPipe()
function, this function will return a. (It's more confusing here, so you may need to think about it)
After obtaining the output, if you need to convert it into a string, you can useCome and get it
Standard output (remember this is
) and then convert to a string.
We can't directlyWrite directly to
Among them, you might say
There are not two ways
ReadFrom
andWriteTo
Is it?
The former can only be read, the latter can only be written to
, so we need a pipeline to
Convert to
, and its contents can only be read or copied. And this conversion is to use the pipeline again, as follows:
var buf r, w, _ := () = w go (r)
here(r)
Goroutine must be used, that is, let the code run in parallel, so add it in front of itgo
。
Here
go (r)
Can also be usedgo (&buf, r)
Substitution, the effect is the same.
Because the order of execution is to start firstcmd2
, and then runcmd
,cmd
After the run is completed, the data flow is passed tocmd2
,cmd2
Run again. otherwisecmd
The standard output when running is empty and will wait.
(r)
rightcmd2
The same is true, but since this is a line of code, it cannot be usedstart
Start it, so just parallelize it.
The following commands are as follows:
() () ()
Here is the process mentioned above:cmd2
Start, runcmd
,letcmd2
waitcmd
output.
One thing to note:go (r)
It can actually be placed in the above code, anywhere except the last line. The reason why it cannot be placed at the end is that it is empty after all running at this time.
Then convert it to a string:
str := ()
Print to see:
("123" + str + "123")
The reason for adding it before and after"123"
This is to avoid printing the contents output to the standard output file as here during debugging.
The results are as follows:
% go run
12360651 ttys010 0:00.00 grep wget
123
It's divided into two lines because when gettinggrep wget
There is a behind\n
, I can't see it here, if%#v
You can see it by formatting and printing.
The complete code is as follows:
func main() { cmd := ("PS", "-A") cmd2 := ("grep", "wget") , _ = () var buf r, w, _ := () = w // The following line of code can be replaced by: go (&buf, r) go (r) () () () str := () ("123" + str + "123") }
This is the end of this article about converting Go into strings. For more related content to convert Go into strings, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!