I'd do what @JimB suggested.
On the other hand, on Linux there's another trick: you can use os/exec.Command()
to execute /proc/self/exe
while telling it to use alternative credentials in the SysProcAttr.Credential
field of the os/exec.Cmd
instance it generates.
See go doc os/exec.Cmd
, go doc syscall.SysProcAttr
and go doc syscall.Credential
.
Make sure that when you make your program re-execute itself, you need to make sure the spawned one has its standard I/O streams connected to those of its parent, and all the necessary opened files are inherited as well.
Another alternatve worth mentioning is to not attempt to bind to port 80 at all and have a proper web server hanging there, and then reverse-proxy either a hostname-based virtual host or a particular URL path prefix (or prefixes) to your Go process listening on any TCP or Unix socket. Both Apache (2.4 at least) and Nginx can do that easily.