SoFunction
Updated on 2025-03-05

Go implementation will convert to strings

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 iscmd, 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 commandcmdThe standard output should be used(), notStdout, as follows (Errors are ignorederrand 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 useStdoutPipe()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 itStandard output (remember this is) and then convert to a string.

We can't directlyWrite directly toAmong them, you might sayThere are not two waysReadFromandWriteToIs it?

The former can only be read, the latter can only be written to, so we need a pipeline toConvert 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

Herego (r)Can also be usedgo (&buf, r)Substitution, the effect is the same.

Because the order of execution is to start firstcmd2, and then runcmdcmdAfter the run is completed, the data flow is passed tocmd2cmd2Run again. otherwisecmdThe standard output when running is empty and will wait.

(r)rightcmd2The same is true, but since this is a line of code, it cannot be usedstartStart it, so just parallelize it.

The following commands are as follows:

()
()
()

Here is the process mentioned above:cmd2Start, runcmd,letcmd2waitcmdoutput.

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 wgetThere is a behind\n, I can't see it here, if%#vYou 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!