在Sublime Text 3中运行Golang项目

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

Run Golang project in Sublime Text 3

问题

我无法在Sublime Text 3中运行Go项目并实时查看输出找到合适的解决方案。

如果我尝试使用以下构建系统:

  1. GoSublime - 我可以使用run命令,它可以运行并停止,但没有输出,而我需要输出。只有当我取消构建时,输出才会出现,这时已经太晚了。

  2. 官方的Golang Build - 我可以构建项目,但仅限于运行一个文件(当前文件),而我需要整个项目。

    我尝试使用run命令的标志,并添加*.go,但是会出现*.go: no such file or directory的错误。

如何在这些解决方案中实时查看输出?我尝试创建自己的构建系统,其中shell_cmdgo run *.go,但是使用取消构建来停止进程不起作用。也许你可以解释一下如何停止正在运行的Go程序?我的mybuild.sublime-build类似于以下内容:

{
    "env": {
        "GOROOT": "/path/gosrc/go",
        "GOPATH": "/path/godev"
    },
    "path": "$PATH:/path/gosrc/go/bin",
    "working_dir": "/path/godev/src/github.com/user/program",
    "cmd": "go run *.go",
    "shell": true
}

..我可以运行Build并查看所需的输出,但是如何停止正在运行的进程呢?

附注:该程序不仅仅是执行并退出 - 它是一个服务,因此我应该在需要的操作发生时看到输出。

英文:

I cannot find a suitable solution for my needs with running a Go project inside a Sublime Text 3 and seeing it's output in real-time.

If I try a build system from:

  1. GoSublime - I can use run, it even runs and stops, but there is no output, which I need. It appears only when I cancel build - it's too late.

  2. Official Golang Build from Go developers - I can build the project, and that's it. It allows to run 1 file (current) only, but I need the whole project.

    I've tried to use flags for run command and to add *.go, but then I get *.go: no such file or directory

How do I see the output in real-time in one of these solutions? I've tried to create my own build system with shell_cmd = go run *.go, but stopping the process with Cancel build is not working then. Maybe you can explain how do I stop a running Go program? My mybuild.sublime-build is similar to this:

{
    "env": {
        "GOROOT": "/path/gosrc/go",
        "GOPATH": "/path/godev"
    },
    "path": "$PATH:/path/gosrc/go/bin",
    "working_dir": "/path/godev/src/github.com/user/program",
    "cmd": "go run *.go",
    "shell": true
}

..I can run Build and see the needed output, but how do I then stop a running process?

P.S. Program is not just executing and exiting - it's a service, so I should see the output when the needed actions happens.

答案1

得分: 1

我个人使用GoSublime和go build .,然后通过第二个命令按名称运行应用程序。显然,这种方法在很多方面都不太好,但大部分时间它还是能够工作的。它提供了一种快速检查编译错误的方式,这正是我所需要的大部分功能。

老实说,只在一个专用的shell中运行会更好。

据我所知,Sublime Text没有更好的Go构建系统可用(这并不是一个认可,它很糟糕,只是比大多数其他系统稍好一些)。

英文:

I personally use GoSublime and go build . followed by running the app by name as a second command. Obviously this sucks in a lot of ways, but it kinda sorta works most of the time. It provides a nice fast way to check for compile errors, which is most of what I need.

Honestly, just running in a dedicated shell is nicer in every way.

AFAIK, there is no better Go build system available for Sublime Text (that isn't an endorsement, it sucks, just less than most).

答案2

得分: 0

我找到了一个教程,可以在Sublime Text 3中运行Go文件,链接在这里:https://www.alexedwards.net/blog/streamline-your-sublime-text-and-go-workflow

如果你使用Build With: Go - Run,你可以得到输出结果,但是如果你在Go中运行一个net/http本地主机,你将无法运行多个程序,取消构建也不起作用。

这是在Sublime Text 3中输出简单的fmt.Println的示例:

> 环境:
>   GOPATH=C:/Users/Christiaan/go
> 目录:C:\Users\Christiaan\Documents_Personal_Learning_Go_test
> 命令:C:\Users\Christiaan\go\go1.15rc1\bin\go.exe run -v C:\Users\Christiaan\Documents_Personal_Learning_Go_test\test2.go
> 输出:
command-line-arguments
gas_pedal: 22314 brake_pedal: 0 steering_wheel: 12562
> 耗时:3.856秒
> 结果:成功
英文:

I found a tutorial where I was able to run Go files on build in Sublime Text 3 here: https://www.alexedwards.net/blog/streamline-your-sublime-text-and-go-workflow

If you use Build With: Go - Run you get outputs, but if you are running for example a net/http local host in Go you won't be able to run multiple programs and cancel build also does not work.

Here is an example of simple fmt.Println output in Sublime Text 3:

> Environment:
>   GOPATH=C:/Users/Christiaan/go
> Directory: C:\Users\Christiaan\Documents_Personal_Learning_Go_test
> Command: C:\Users\Christiaan\go\go1.15rc1\bin\go.exe run -v C:\Users\Christiaan\Documents_Personal_Learning_Go_test\test2.go
> Output:
command-line-arguments
gas_pedal: 22314 brake_pedal: 0 steering_wheel: 12562
> Elapsed: 3.856s
> Result: Success

huangapple
  • 本文由 发表于 2017年8月27日 23:35:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/45906619.html
匿名

发表评论

匿名网友

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

确定