I am simply reading the /proc/diskstats
file. My code is:
func ReadFromFile(filepath string)(string){
defer func() {
if err1 := recover(); err1 != nil {
fmt.Println("!!!!!!!!!!!!!!!!Panic Occured and Recovered in readFromFile(), Error Info: ", err1)
}
}()
strData := ""
data, err := ioutil.ReadFile(filepath)
if err != nil{
fmt.Println("File read error: ", err)
return ""
}
strData = string(data)
return strData
}
The error I am getting is:
File read error: open /proc/diskstats: too many open files
Not only for this file, I am also getting the same error for some other files.
I have also run this command:
root@golang:~# lsof|wc -l
785
Please guide me.
I ran into the same problem (maybe different circumstances or setup) and fixed it differently:
func some_func(file_name []string) {
for _, file_name := range file_names {
f, _ := os.Create(file_name)
// defer f.Close() // bad idea
_, _ = f.Write([]byte("some text"))
f.Close() // better idea
}
}
The problem is that defer
will be executed, when the function will return, which could take a while - depending on the loop size (bad idea). So just do it explicit (better idea )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With