打开新的控制台窗口

What is some code I can use so that my golang program can open a new console window?

I have one program A running program B, and so there is no console window for program B (the stdin and stdout is used for interaction with program A). I want program B to display user-friendly information in a new console window (along with program A’s console window) so that it is easy to see what is going on.

The below code would work on Ubuntu, it is a sample program that echoes the input unless you type "new console" in which case executes itself again in a separate console.

It does that by executing:

gnome-terminal -e <program>

Note that this might be OS specific, as each OS flavour would have a different way of opening a new shell window.

For example, in Windows it seems the same can be achieved using:

cmd /C start <program>

(though I am not able to test that right now)

Sample working program for Ubuntu:

package main

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

func main() {
    progPath, err := os.Executable()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(`Enter text to echo (or "new console" to open a new console)`)
    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
        text := scanner.Text()
        if text == "new console" {
            cmd:= exec.Command("gnome-terminal","-e", progPath)
            cmd.Start()
        } else {
            fmt.Println(scanner.Text())
        }
        fmt.Println(`Enter text to echo (or "new console" to open a new console)`)
    }
}