简体   繁体   English

在GoLang中杀死ListenAndServe

[英]Kill ListenAndServe in GoLang

I am trying to use Sublime Text + GoSublime to do Golang development. 我正在尝试使用Sublime Text + GoSublime进行Golang开发。

I am working with a webserver in Go ( ListenAndServe("8000", &handler) ). 我正在使用Go中的Web服务器( ListenAndServe("8000", &handler) )。

The problem I have is that when I run the Go process (while another one is running), the second process doesn't work (since the port is already in use). 我的问题是,当我运行Go进程(同时正在运行另一个进程)时,第二个进程不起作用(因为该端口已在使用中)。

What I want to happen is that when I go run web.go , if one was already running, shut that down before running this one. 我想发生的是,当我go run web.go ,如果已经运行了它,请在运行它之前将其关闭。

Sublime Text 3's build system doesn't seem to keep track of the previous execution - so I don't know the best way to terminate. Sublime Text 3的构建系统似乎无法跟踪先前的执行-因此,我不知道终止的最佳方法。 When I run CTRL+B it doesn't block, it executes the build system in another thread - meaning that I can't CTRL+C before executing the next. 当我运行CTRL + B时​​,它不会阻塞,它会在另一个线程中执行构建系统-这意味着在执行下一个之前,我无法按CTRL + C。 So what I'm having to do is terminate via the command line the existing process before starting the next one. 因此,我要做的是在启动下一个进程之前,通过命令行终止现有进程。

So I'm looking for either a solution with Sublime Text's build system configuration, or perhaps even having the Go process itself check to see if one is running and killing the process. 因此,我正在寻找具有Sublime Text的构建系统配置的解决方案,或者甚至让Go进程本身检查是否正在运行并终止该进程。

Ideas? 有想法吗? It's sort of annoying to swap back and forth from terminal. 从终端来回交换有点烦人。

Not sure if I understand the question right. 不知道我是否理解正确的问题。

But in sublime text build console, there is tskill command which can kill active tasks initiated from the console. 但是在崇高的文本构建控制台中,有tskill命令可以杀死从控制台启动的活动任务。 So you can type Ctrl+B to invoke out console and type tskill to kill the running web.go task. 因此,您可以键入Ctrl+B来调出控制台,并键入tskill来杀死正在运行的web.go任务。 There are also other alias tskill <task ID>, task replay or task go . 还有其他别名tskill <task ID>, task replay or task go Reference is here . 参考在这里 After that you can run other commands including go run. 之后,您可以运行其他命令,包括go run。

What's more there is a replay command. 此外,还有一个replay命令。 "It is like run but attempts to cancel any active executions before running". “这就像运行,但是尝试在运行之前取消任何活动的执行”。 I feel it looks like what you want. 我觉得它看起来像您想要的。

Further, Gosublime command shell has sh command mode. 此外,Gosublime命令外壳具有sh命令模式。 It's much flexible for example type sh killall web;go run web.go or run a shell script to run that. 它非常灵活,例如键入sh killall web;go run web.go或运行Shell脚本来运行它。

Hopefully it helps. 希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM