Golang os.File.Chdir() function example

package os

Golang os.File.Chdir() function usage example

NOTE : The binary could be executing from somewhere else and changing to the target directory of the file can make life easier for file level operations such as combining different files or splitting files.

 file, err := os.Open("/anotherdir/fileinthisdirectory.txt")
 if err != nil {
 fmt.Println(err)
 continue
 }
 err = file.Chdir() // change current working directory to the file's directory - /anotherdir
 file.Close()

See also : https://www.socketloop.com/tutorials/golang-how-to-split-or-chunking-a-file-to-smaller-pieces

References :

http://golang.org/pkg/os/#File.Chdir

Advertisement