英文:
Communication with other Go process
问题
我有一个程序,从控制台读取文件名并执行go run filename.go
。
// main.go
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
)
func main() {
console := bufio.NewReader(os.Stdin)
fmt.Print("输入文件名:")
input, err := console.ReadString('\n')
if err != nil {
log.Fatalln(err)
}
input = input[:len(input)-1]
gorun := exec.Command("go", "run", input)
result, err := gorun.Output()
if err != nil {
log.Println(err)
}
fmt.Println("---", input, "执行结果 ---")
fmt.Println(string(result))
}
在同一个目录下,我有另一个文件,内容如下:
// hello.go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
当我在控制台输入"hello.go"时,该文件会被执行,并将其输出返回给父进程。然而,我还有另一个类似的程序:
// count.go
package main
import (
"fmt"
"time"
)
func main() {
i := 0
for {
time.Sleep(time.Second)
i++
fmt.Println(i)
}
}
由于这个程序永远不会返回,我的父进程会一直挂起。有没有办法与不同的Go进程进行通信?我在想是否可以像goroutine的通道那样,为进程提供一种通信方式。我需要能够接收子进程的实时标准输出。
我试图解决的问题是从目录动态执行Go程序。每天都会添加、删除和修改Go文件。我有点像要创建类似Go Playground的东西。主进程是一个提供网页的Web服务器,所以我不能一直关闭它来修改代码。
英文:
I have a program that reads a filename from the console and executes go run filename.go
.
// main.go
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
)
func main() {
console := bufio.NewReader(os.Stdin)
fmt.Print("Enter a filename: ")
input, err := console.ReadString('\n')
if err != nil {
log.Fatalln(err)
}
input = input[:len(input)-1]
gorun := exec.Command("go", "run", input)
result, err := gorun.Output()
if err != nil {
log.Println(err)
}
fmt.Println("---", input, "Result ---")
fmt.Println(string(result))
}
In the same directory, I have another file like this.
// hello.go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
When I input "hello.go" in the console, that file is run, and its output gets returned to the parent Go process. However, I have another program like this.
// count.go
package main
import (
"fmt"
"time"
)
func main() {
i := 0
for {
time.Sleep(time.Second)
i++
fmt.Println(i)
}
}
Except, because this program never returns, my parent process is left hanging forever. Is there a way to communicate with different Go processes? I'm thinking something like channels for goroutines, but for processes. I need to be able to receive live stdout from the child process.
The problem I'm trying to solve is dynamically executing Go programs from a directory. Go files will be added, removed, and modified daily. I'm kind of trying to make something like Go Playgrounds. The main process is a webserver serving webpages, so I can't shut it down all the time to modify code.
答案1
得分: 1
不要使用go run
,你需要自己完成go run
所做的工作,以使Go程序成为服务器进程的直接子进程。
使用go build -o path_to/binary source_file.go
将给你更多的控制权。然后,你可以直接执行并与生成的二进制文件进行通信。
英文:
Don't use go run
, you need to do what go run
is doing yourself to have the go program be a direct child of your server process.
Using go build -o path_to/binary source_file.go
will give you more control. Then you can can directly execute and communicate with the resulting binary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论