关闭浏览器后停止Go服务器

I'm running a simple Go server on a OSX machine. When I run the command to run the Go program it starts the server and open a browser window.

I would like to know if there's a way to when I close that browser window (by the window, not the program) also close the server and quit the terminal.

Thank you in advance.

You could let your browser emit a message before dying, or use a web socket, but a simple, reliable and usual way to do that kind of stuff is to set up a Javascript timer to have the browser send a heart-beat message to the server every now and then (depending on how quickly you want the server to stop after the browser's closing ; every second, every 15s, every minute... ?). When the server figures that a due message has not arrived, it can exit.