如何使用exec和syscall软件包golang传递端口值以启动服务器

We are trying to implement a program in go which runs another go program from the specified path like

path, _ := exec.LookPath("program-name")

Next we have given a set of go commands to run the go program like

args := []string{"go", "install", "&&", "-port", "18000"}

We passed both path and args along with os.Environ() to the syscall.Exec(). In order to run the project which we are calling has a check which tells us -port is required. As -port is not an executable command so it not taking the port value.

The requirement is when we type go install && project-name -port 19000 the program should run.

The query is there any way to achieve this and how can we pass the port value to get the desired outcome.

This does not work for two reasons

args := []string{"go", "install", "&&", "-port", "18000"}

First, "project-name" is missing. I assume this is a simple typo.

Second, syscall Exec does not use a shell to launch commands. The && construct and running multiple commands from one line are shell features

To make syscall Exec do this you could call it, check the return value and then call it again with the second command

Or you could use syscall Exec to launch a shell. Below there is an example of launching a shell - (with os/exec, but this is very similar in this case) to run two commands with a &&

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    args := []string{"-c", "touch a && ls -l"}
    cmd := exec.Command("bash", args...)
    stuff, err := cmd.Output()
    fmt.Printf("Command finished with error: %v", err)
    fmt.Printf("%s", string(stuff))
}