Mac/Linux and Golang : Fix bind: address already in use error
Not really a tutorial. However, it is good to put this here as a guide for junior software developers.
Saw this message in one of the Facebook group that I'm helping out as administrator:
I'm trying to create a simple REST API using go. Everytime I save and run the file, I get this error
"bind: address already in use"
I then have to go and change the port address. Problem is that everytime I make a change to the file it gives the error. So on making successive changes to the file I have to successively come up with port addresses and change that in the file
The OP is struggling to understand why his/her program works when running for the first time and then unable to run for the second time.
Diagnostic:
Because the OP did not show his/her source code, I can only speculate that the program that the person trying to run stayed in memory and hogged the port address. The left over process(program) is also known as zombie process.
Solution:
If on Linux, try using this command to find out the process ID.
$sudo lsof -n -i :<port number>
On MacOSX:
$lsof -i tcp:<port no>
Once you find out the PID(process identifier) of your zombified Golang program, kill it with
$kill -9 <PID>
Try to relaunch the program again with the same port address after killing the hoggers and it should run with complaining:
"bind: address already in use"
See also : nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+31.6k Golang : Convert an image file to []byte
+16.7k Golang : How to generate QR codes?
+12.1k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+21.9k Golang : How to run Golang application such as web server in the background or as daemon?
+16.9k Golang : Get number of CPU cores
+9.7k Golang : ffmpeg with os/exec.Command() returns non-zero status
+4.6k Unix/Linux : How to pipe/save output of a command to file?
+6.8k Golang : constant 20013 overflows byte error message
+23.9k Golang : Find biggest/largest number in array
+6.6k Golang : Join lines with certain suffix symbol example
+8.9k Golang : How to capture return values from goroutines?
+11.4k Golang : Display a text file line by line with line number example