> For the complete documentation index, see [llms.txt](https://666isildur.gitbook.io/ethical-hacking/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://666isildur.gitbook.io/ethical-hacking/programming/golang/execute-commands.md).

# Execute Commands

Simple script to learn how to execute commands and re-use later on future programs.

```go
package main

// usage: go run execmd.go -cmd <command>
// how to execute commands on a system (windows or linux)

import (
	"log"
	"os"
	"os/exec"
)

func executeCommand(command string, argsArray []string) (err error) {
	args := argsArray
	// create object
	cmdObj := exec.Command(command, args...)
	// stdout to display the output on the screen
	cmdObj.Stdout = os.Stdout
	// process errors
	cmdObj.Stderr = os.Stderr
	// run the command
	err = cmdObj.Run()

	if err != nil {
		log.Fatal(err)
		return
	}
	return nil
}

func main() {
	cmd := flag.String("cmd", "", "Select command to use")
	flag.Parse()
	command := "sudo"
	executeCommand(command, []string{*cmd})

}
```

{% hint style="info" %}
To learn: How to parse more than one argument on the same flag
{% endhint %}
