Hmm, is there really no way to do this in a cross-platform way? I'd like to figure out that I'm near the open file limit, so I can stop accepting more work, and I'd rather do that before I hit the file limit & start generating errors. I suppose I could read the proc filesystem, but that doesn't work on my dev machine (a mac).
Hah, that just occurred to me about 10 minutes ago. :) It's better than nothing, I just find it kind of weird that this seems to be nearly impossible from Go, although I guess that's because there's really no good way to do it from C either. It looks like you can play some tricks with poll() to figure out which handles are open, but it's ugly too.
For future reference for anyone else trying to figure this out, here's my basic solution:func countOpenFiles() int {out, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("lsof -p %v", os.Getpid())).Output()if err != nil {log.Fatal(err)
}lines := strings.Split(string(out), "\n")return len(lines) - 1
}
If you're on Linux, and aren't too fussed about portability, you could read /proc/self,fd ?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.