How to Read last lines from a big file with Go every 10 secs -


how can read last 2 lines big log file without load memory completely?

i need read every 10 secs(on win machine)...and i'm stuck trying read last lines..

package main  import (     "fmt"     "time"     "os" )  const myfile = "logfile.log"  func main() {     c := time.tick(10 * time.second)     := range c {         readfile(myfile)     } }  func readfile(fname string){     file, err:=os.open(fname)     if err!=nil{         panic(err)     }     buf:=make([]byte, 32)     c, err:=file.readat(32, ????)     fmt.printf("%s\n", c)   } 

the log file like:

07/25/2013 11:55:42.400, 0.559 07/25/2013 11:55:52.200, 0.477 07/25/2013 11:56:02.000, 0.463 07/25/2013 11:56:11.800, 0.454 07/25/2013 11:56:21.600, 0.424 07/25/2013 11:56:31.400, 0.382 07/25/2013 11:56:41.200, 0.353 07/25/2013 11:56:51.000, 0.384 07/25/2013 11:57:00.800, 0.393 07/25/2013 11:57:10.600, 0.456 

thanks!

you can use file.seek() or file.readat() end , reading forward. can estimate start seeking unless can know 2 lines = x bytes.

you can file length using os.stat(name)

here example based on readat, stat, , sample log file:

package main  import (     "fmt"     "os"     "time" )  const myfile = "logfile.log"  func main() {     c := time.tick(10 * time.second)     _ = range c {         readfile(myfile)     } }  func readfile(fname string) {     file, err := os.open(fname)     if err != nil {         panic(err)     }     defer file.close()      buf := make([]byte, 62)     stat, err := os.stat(fname)     start := stat.size() - 62     _, err = file.readat(buf, start)     if err == nil {         fmt.printf("%s\n", buf)     }  } 

Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -