TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang os exec Examples: Command Start and Run

Use the os/exec package, create Commands and use Start and Run on them. Pass arguments to the Command.
Os Exec, Command. Often a program needs to start other programs. Not everything can be done in a single Golang program. So we can invoke external processes.
We can import the os/exec package. Then we create a Command with 1 or 2 arguments (we pass the process name, and its arguments in separately).
An example. To begin, we Start a Firefox process. You will need to adjust the path to the Firefox application yourself—on Linux this is firefox-bin.

Start: We call the Start func, and a Firefox window pops upon the computer. Start() does not wait for the process to end.

Golang program that uses Command, Start package main import ( "fmt" "os/exec" ) func main() { // Create command. // ... The application is not executed yet. cmd := exec.Command("/home/sam/Downloads/firefox/firefox-bin") fmt.Println("Starting command") // Run firefox-bin. // ... This starts a web browser instance. // It does not wait for it to finish. cmd.Start() fmt.Println("DONE") } Output Starting command DONE
Argument. Here we start an external process with an argument. We pass the argument string to the Command, after the process name itself.

Here: We open Firefox and view Wikipedia's English home page. The next several hours of your time might disappear quickly.

Golang program that uses Command with argument package main import ( "fmt" "os/exec" ) func main() { // New command with 1 argument. browser := "/home/sam/Downloads/firefox/firefox-bin" argument := "https://en.wikipedia.org/"; cmd := exec.Command(browser, argument) // Run firefox-bin and load URL specified in argument. cmd.Start() fmt.Println("DONE") } Output DONE
Run. Unlike Start, the Run() func waits for the process to end before returning. Here we Run() a Python program. When the program ends, we read its exit value (which is 100 here).
Golang program that uses Command with Run package main import ( "fmt" "os/exec" ) func main() { // Run this Python program from Go. cmd := exec.Command("python", "/home/sam/test.py") fmt.Println("Running") // Wait for the Python program to exit. err := cmd.Run() fmt.Println("Finished:", err) } Output Running Finished: exit status 100 Python program: # This is a Python program. exit(100)
A summary. With Command, we launch external processes like Firefox and Python (or any EXE or script). We must invoke Start() or Run() to execute the code.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf