I'm working on code in Go that will install a program. It has to run Linux and Windows systems. I am using Intellij.
if isWindows() {
cmd := exec.Command("msiexec.exe", "/i", filename, "/quiet", "L*V", installLog)
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
}
if err := cmd.Start(); err != nil {
log.Printf(failedInstallMsg, filename, err)
return
}
} else if isLinux() {
cmd := exec.Command("sudo", "-n", packageManagerTool, "--disablerepo=*", "install", "-y", filename)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Pgid: 0,
}
if err := cmd.Start(); err != nil {
log.Printf(failedInstallMsg, filename, err)
return
}
}
}
My issue is that since in the IDE Go Project settings > Build tags the OS is set to Linux then the syscall.SysProcAttr struct fields are only available for Linux. This means the line in the windows case that initialize the struct with
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
}
causes a compilation error
unknown field CreationFlags in struct literal
I tried adding
// +build linux windows
At the top of the file but it doesn't help.
Is there an easy way to have code for both windows and linux co-exist in the way described above?