Golang : Launching your executable inside a console under Linux
Problem: You want to launch your executable under a terminal window after clicking on an icon. Why you want to do that? Because not everyone knows how to navigate the terminal and use the command line interface. Your program end-user prefers to point-n-click to launch your program. So how to open a terminal first and execute your program inside the terminal?
Consider this program:
package main
import (
"fmt"
"os/exec"
)
func main() {
exec.Command("xdg-open", "https://www.socketloop.com").Start()
fmt.Println("Can you read this line??!")
}
What it does when run inside a terminal, it will open the default browser and point to the given URL. Try launching this program by clicking on the executable icon, you will still get the browser opened and pointing to the given URL. Trouble is, you won't be able to see the output after exec.Command()
.
Solution: Wrap your executable inside a script file and use it to launch your application. The end-user will still be able the see the logging or what output your program send to the console. Once the user is done with your application, he/she can just close the terminal window.
First, create a script to launch your binary. For example, launchmyserver.sh
:
#!/bin/bash
./my-server-executable-file
read -s -n1 -p "Press any key to exit..."
Next, make the script executable with
chmod +x launchmyserver.sh
Finally, click on launchmyserver.sh
icon instead of the executable icon and select "Run In Terminal" option.
Hope this helps! Happy coding!
Reference:
See also : Golang : Execute shell command
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+19.4k Golang : Set or Add HTTP Request Headers
+9k Golang : How to get username from email address
+6.5k Golang : Output or print out JSON stream/encoded data
+9.2k Golang : Accessing content anonymously with Tor
+11.6k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+8.1k Your page has meta tags in the body instead of the head
+5.7k Golang : Use NLP to get sentences for each paragraph example
+11.1k Golang : Characters limiter example
+13.7k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+13.5k Golang : Check if an integer is negative or positive
+18.6k Golang : Padding data for encryption and un-padding data for decryption
+10k Golang : cannot assign type int to value (type uint8) in range error