从终端关闭服务器的正确方法是什么?

I am trying my hands on some basic chat (server + client) stuff in Go so I have a server which listens on a specific port and I have a client which writes to this port.

However, as I am new to this, I constantly make changes and have to restart the server etc. I've been doing 'Ctrl + C' everytime I want to stop server but this is obviously stupid as I have to change the port number on every compilation... What is the correct way of doing this? I'm currently just doing

defer ln.Close()

in the main function of the server after the connection has been established but I guess Ctrl + C just kills the process without closing the connection?

EDIT: More information. I am running cygwin on Windows. ps shows no old processes but I found a looot of "server.exe" (my server file is named server.go) in the task manager.

Unless you are using os/signal package to Notify you when you hit Ctrl+C your defer statement will not get run.

Here is an example of a handled SIGINT (Ctrl+C) to exit a program cleanly.

func main() {
    done := make(chan os.Signal)
    go signal.Notify(done, syscall.SIGINT)

    go func() {
         // your tcp server goes here along with the defer to clean up your server
    }()

    <-done

    // exit cleanly
}