go - Remove first line from text file in Golang -


i'm trying pop first line of file , reduce file lines 1 one. implementation removing first line follows

type fs struct {     ...     file       *os.file }  //file creation ok...   func (fs *fs) pop() []byte {     var buf []string     scanner := bufio.newscanner(fs.file)     //reading lines     scanner.scan() {         line := scanner.text()         buf = append(buf, line)     }     //writing second line on same file     s := 1; s < len(buf); s++ {         fs.file.writestring(fmt.println(buf[s]))     }     //commit changes     fs.file.sync()     fs.file.close()      return []byte(buf[0]) } 

i returned []byte expected string file never changes. missing here?

for example,

package main  import (     "bytes"     "fmt"     "io"     "os" )  func popline(f *os.file) ([]byte, error) {     fi, err := f.stat()     if err != nil {         return nil, err     }     buf := bytes.newbuffer(make([]byte, 0, fi.size()))      _, err = f.seek(0, os.seek_set)     if err != nil {         return nil, err     }     _, err = io.copy(buf, f)     if err != nil {         return nil, err     }     line, err := buf.readstring('\n')     if err != nil && err != io.eof {         return nil, err     }      _, err = f.seek(0, os.seek_set)     if err != nil {         return nil, err     }     nw, err := io.copy(f, buf)     if err != nil {         return nil, err     }     err = f.truncate(nw)     if err != nil {         return nil, err     }     err = f.sync()     if err != nil {         return nil, err     }      _, err = f.seek(0, os.seek_set)     if err != nil {         return nil, err     }     return []byte(line), nil }  func main() {     fname := `popline.txt`     f, err := os.openfile(fname, os.o_rdwr|os.o_create, 0666)     if err != nil {         fmt.println(err)         return     }     defer f.close()     line, err := popline(f)     if err != nil {         fmt.println(err)         return     }     fmt.println("pop:", string(line)) } 
 $ cat popline.txt lorem ipsum dolor sit amet, consectetur adipiscing elit,  sed eiusmod tempor incididunt ut labore et dolore magna aliqua.  ut enim ad minim veniam,  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.  duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.  excepteur sint occaecat cupidatat non proident,  sunt in culpa qui officia deserunt mollit anim id est laborum.  $ go run popline.go pop: lorem ipsum dolor sit amet, consectetur adipiscing elit,   $ cat popline.txt sed eiusmod tempor incididunt ut labore et dolore magna aliqua.  ut enim ad minim veniam,  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.  duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.  excepteur sint occaecat cupidatat non proident,  sunt in culpa qui officia deserunt mollit anim id est laborum.  $  

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -