exec.Command()
does not return an error
but *Cmd
type struct on which you then can call a method Run()
(or other methods, see NOTE below) to execute the command like this:
cmd := exec.Command("cmd","/c","nmap -T4 -A -v localhost")
err := cmd.Run()
if execErr != nil {
log.Fatal(err)
}
Your code panic
s because exec.Command("cmd","/c","nmap -T4 -A -v localhost")
returns a valid non-nil pointer to Cmd
struct (panic message is likely full path to the cmd.exe
and then arguments you provided which are values of fields Path
and Args
of Cmd
struct...).
NOTE: It is not good practice to panic()
here, use log.Fatal()
instead. Also it might be better to run nmap
directly and process its output in code. See examples of reading other command output using Output()
, CombinedOutput()
and Start()
depending on what you are trying to do and how command behaves.