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

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.