Pages

Showing posts with label Golang. Show all posts
Showing posts with label Golang. Show all posts

Monday, September 18, 2023

shell bultin vs external shell

 It is advised that "which" is not a native (builtin) command in Linux. We might use the command "type" to check its origin.

In Bash, some commands such as "hash", "type", and "command -v" are recommended over "which" to utilised in case you want to check whether a particular application has been installed on the machine or not.

By the way, you might use "cmdOut, err := exec.Command("sh", "-c", input).Output()" in Golang to run external shell. I write a simple utility in Golang as bellow to do that:

If you write exec.Command("ls"), it will surely work but exec.Command("type type") it will throw an error saying that "type" is not a command. 

Here is the detailed error: "exec: "type netstat": executable file not found in $PATH".

Changing to "exec.Command("sh", "-c", "type netstat").Output() it will work perfectly.



Friday, September 15, 2023

log.Fatalf() vs fmt.Printf() or fmt.PrintLn() in Golang

The former will stop the application/process whereas the latter will keep running as normal. That sounds simple but critical when writing your Golang code.