I'm trying to create a log file on linux /var/log directory, but got permission denied. Any best practices without having to change the ownership of the directory?
f, _ := os.Create("/var/log/go_server.log")
defer f.Close()
log.SetOutput(f)
What you have there is a standard UNIX permissions issue. Given the special nature of that directory, you've got three options:
Note also that systemd can save your stdout/err to files if you configure it right and you can then browse with journalctl. Indeed, leaving your program to stupidly print diags to stdout/err and not forking itself is the smartest thing to do now that systemd does all that stuff for you (that way, you can focus on what your program does and not reinventing the wheel wrt daemonization and logging).
For all the grief systemd gets, it's actually pretty good at this use case.