douluxia0606 2014-03-20 18:38
浏览 122
已采纳

在GoLang中杀死ListenAndServe

I am trying to use Sublime Text + GoSublime to do Golang development.

I am working with a webserver in Go (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).

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.

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. 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. 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.

Ideas? It's sort of annoying to swap back and forth from terminal.

  • 写回答

1条回答 默认 最新

  • dtq26360 2014-03-21 05:42
    关注

    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. So you can type Ctrl+B to invoke out console and type tskill to kill the running web.go task. There are also other alias tskill <task ID>, task replay or task go. Reference is here. After that you can run other commands including go run.

    What's more there is a replay command. "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. It's much flexible for example type sh killall web;go run web.go or run a shell script to run that.

    Hopefully it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址