how to use Golang to shell command with given sudo password
04:42 05 Jul 2021

I'd like to use a piece of command line to set datetime in Go, but the following code failed

datetime := "2021-06-17 18:20:41.8"
sudoPassword := "xxxxx"
app := "echo"
arg0 := sudoPassword
arg1 := "|sudo -S"
arg2 := "date"
arg3 := "-s"
arg4 := "\"" + datetime + "\""
cmd := exec.Command(app, arg0, arg1, arg2, arg3, arg4)

Is there a correct way to do this? Fill the password automatically like in Python

os.system('echo %s|sudo -S %s' % (sudoPassword, command))
shell datetime go command sudo