golang opening second terminal/console

huangapple go评论73阅读模式
英文:

golang opening second terminal/console

问题

我一直在寻找,但找不到类似的关于golang的问题。

在golang中有没有一种方法可以打开第二个控制台/终端窗口并将输出发送到其中?
我基本上想在一个终端中输入,而另一个终端则会不断更新信息,以便程序在需要时可以更新窗口中的内容,因此我不想覆盖用户当前在第一个终端中输入的内容。

更新:

我一直在思考一些想法,并且通过exec函数,你可以运行终端命令,比如Windows的"start"函数,它可以打开另一个终端。这是我目前为止的进展,现在我只需要添加一个管道,使可执行文件"node.exe"可以从中读取。这显然是使用exec库中的cmd结构来完成的。一旦我完成了,我会更新并发布我的答案。

package main

import (
	"log"
	"os/exec"
)

func main(){
	cmd := exec.Command("cmd","/C","start","node.exe")
	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	}
	for i := 0; i < 100; i++{
		log.Println(i)
	}
}

这将在另一个终端中运行node.exe,同时在当前终端中循环打印0-99。

英文:

I have been looking around and can't find another question like this for golang.

Is there a way in golang to open a second console / terminal window and send output to it?
I basically want to use one terminal for typing in, and the other to have a constant feed of information that will update in the window whenever the program wants, so I don't want to overwrite what a user is currently typing into the first terminal.

Update:

I have been working on some ideas, and with the exec function, you can run terminal commands, such as the windows "start" function, which opens another terminal and. This is as far as I have made it, now I just need to add a pipe so that the executable "node.exe" will read from it. This apparently is done using the cmd structure in the exec library. I will update, once I get it all i'll post my answer.

package main

import (
	&quot;log&quot;
	&quot;os/exec&quot;
)

func main(){
	cmd := exec.Command(&quot;cmd&quot;,&quot;/C&quot;,&quot;start&quot;,&quot;node.exe&quot;)
	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	}
	for i := 0; i &lt; 100; i++{
		log.Println(i)
	}
}

This runs node.exe in another terminal, meanwhile it loops through and prints 0-99 to the current terminal

答案1

得分: 2

澄清一下,“打开一个新的控制台窗口”似乎非常依赖于操作系统。

那么,为什么不将日志信息输出到文件中,然后打开一个新窗口并在该文件上运行tail命令呢?

一般来说,如果你有重要的日志信息,它应该被记录到文件中。所以这部分只是一个好的起点。一旦你有了日志文件,对该文件运行tail命令是完全合理的。

你正在构建的基本上是一个“客户端/服务器”模式。这种行为的常规方式是提供一个“服务器”程序,然后通过一个“客户端”程序连接到它。只需看看MongoDB、Redis或MySQL,它们都附带了一个运行服务器的守护进程和一个单独的客户端。

根据你的描述,你正在尝试同时做这两个,并将其作为默认行为。即使这样能够工作,对于其他人来说,这似乎很奇怪。而且你需要处理奇怪的情况。比如,如果我复制“服务器”部分(即“日志窗口”)并访问该窗口,然后按下CTRL+C,那么会杀死服务器吗?第一个窗口中的客户端会发生什么?

即使你设法使其工作,试图同时做两件事情会变得混乱。

英文:

To clarify, this "open a new console window" seems very OS-specific.

That stated, why not just output logging information to a file and then open a new window and run tail on that file?

In general, if you're going to have important logging information, it should go to a file. So that part is just good practice to start with. And once you have that log file, running tail on the file seems perfectly reasonable.

What you're basically building here is a "client/server" pattern. The norm for this behaviour is to provide a "server" program and then connect to it via a "client" program. Just take a look at MongoDB or Redis or MySQL, they ship with some daemon that runs the server and a separate client.

Based on your description, you're trying to do both at once and make that the default behaviour. Even if this works, it's going to seem weird to anyone else trying to use the program. And you'll need to handle weird cases. Like what happens if I fork the "server" portion (logging window) and then visit that window and hit CTRL+C? Does that kill the server? What happens to the client in the first window?

Trying to do 2 in 1 is going to be messy even if you manage to make it work.

huangapple
  • 本文由 发表于 2014年10月17日 10:17:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/26416887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定