Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Golang: Give an array in exec.Command()

So basically I was making a golang program using os/exec

I have an array of some strings which I wanna pass as a Command

args := ["alacritty", "--needed", "--dlpkg"]

cmd := exec.Command("pkexec", "pacman", "-S", "--noconfirm")
    cmd.Stdout = os.Stdout
    cmd.Stdin = os.Stdin
    err := cmd.Run()

I need to pass the args after the –noconfirm string but everytime I want to put the array it asks for a ...string type. Is there any way to convert a string array to ...string. Or any other method to pass the array correctly through exec.Command()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can put all values in a slice by using ...

args := []string{"pacman", "-S", "--noconfirm", "alacritty", "--needed", "--dlpkg"}

cmd := exec.Command("pkexec", args...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
err := cmd.Run()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading