如何在Golang中实现类似Python subprocess.run的STDIN输入功能?
Replicating Python's
subprocess.run with stdin input in Go If you’re used to Python’s handy subprocess.run with the input parameter, translating that behavior to Go is totally straightforward once you know how to work with the command’s stdin pipe. Here’s exactly how to replicate your Python call subprocess.run(['kinit', username], input=password.encode()) in Go:
Full Code Example
package main import ( "bytes" "log" "os/exec" ) func main() { username := "your-actual-username" password := "your-actual-password" // Create the command—matches the Python list argument exactly cmd := exec.Command("kinit", username) // Prepare the password input as a bytes buffer (since Stdin accepts any io.Reader) inputBuffer := bytes.NewBufferString(password) cmd.Stdin = inputBuffer // Optional: Capture stdout and stderr if you need to inspect them later var stdout, stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr // Run the command and handle any execution errors err := cmd.Run() if err != nil { log.Fatalf("Command failed: %v\nError output: %s", err, stderr.String()) } // Use the output if needed log.Printf("Command succeeded! Output: %s", stdout.String()) }
Key Breakdown
Let’s map this directly to your Python code:
exec.Command("kinit", username)is the Go equivalent of['kinit', username]—it creates the external command you want to run.- Instead of Python’s
inputparameter, we setcmd.Stdinto our input source. Converting the password string to abytes.Bufferworks just like passing the encoded bytes in Python, sinceStdinaccepts anyio.Readertype. cmd.Run()executes the command and waits for it to finish—matching the default behavior ofsubprocess.run(which waits by default instead of running in the background).
Alternative: Explicit Stdin Pipe Writing
If you prefer a more hands-on approach (great for incremental input), you can use cmd.StdinPipe() to get a writer and send the input directly:
package main import ( "io" "log" "os/exec" ) func main() { username := "your-actual-username" password := "your-actual-password" cmd := exec.Command("kinit", username) // Get the stdin pipe to write to stdin, err := cmd.StdinPipe() if err != nil { log.Fatal(err) } // Start the command first (critical when using StdinPipe) if err := cmd.Start(); err != nil { log.Fatal(err) } // Write the password to the command's stdin _, err = io.WriteString(stdin, password) if err != nil { log.Fatal(err) } // Close the pipe to signal we're done sending input stdin.Close() // Wait for the command to complete if err := cmd.Wait(); err != nil { log.Fatalf("Command failed: %v", err) } }
This method is useful if you need to send input in chunks rather than all at once.
Quick Notes
- Always handle errors explicitly—Go won’t let you ignore them silently like Python might in some scenarios.
- If you don’t need to capture stdout/stderr, you can skip those lines, and the command will use your parent process’s console output by default.
内容的提问来源于stack exchange,提问作者Sumit Ghosh
相关产品推荐
相关产品推荐

