Ben Chuanlong Du's Blog

It is never too late to learn.

Manipulate Filesystem in Golang

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

In [1]:
import (
    "bufio"
    "fmt"
    "os"
)
In [10]:
fileInfo, err := os.Stat("manipulate-filesystem-in-golang.ipynb")
fileInfo
Out[10]:
&{manipulate-filesystem-in-golang.ipynb 5209 436 {688987354 63793284779 0x219c440} {2050 38010897 1 33204 1000 1000 0 0 5209 4096 16 {1657639454 51336028} {1657687979 688987354} {1657687979 688987354} [0 0 0]}}
In [12]:
fileInfo.Mode()
Out[12]:
-rw-rw-r--
In [16]:
os.Chmod("test.txt", 0777)
In [17]:
fileInfo, err := os.Stat("test.txt")
fileInfo
Out[17]:
&{test.txt 0 511 {613146344 63793284812 0x219c440} {2050 38010904 1 33279 1000 1000 0 0 0 4096 0 {1657688012 741146953} {1657688012 613146344} {1657688218 54021852} [0 0 0]}}
In [18]:
fileInfo.Mode()
Out[18]:
-rwxrwxrwx
In [19]:
os.Chmod("test.txt", 0664)
In [20]:
fileInfo, err := os.Stat("test.txt")
fileInfo
Out[20]:
&{test.txt 0 436 {613146344 63793284812 0x219c440} {2050 38010904 1 33204 1000 1000 0 0 0 4096 0 {1657688012 741146953} {1657688012 613146344} {1657688251 198147320} [0 0 0]}}
In [21]:
fileInfo.Mode()
Out[21]:
-rw-rw-r--
In [ ]:
os.Create
In [ ]:
os.Write

Copy File

The GoLang standard library does not provide a function to copy files directly. However, you can copy a file using ioutil.ReadFile + ioutil.WriteFile.

(*File).WriteString

WriteString is like Write, but writes the contents of string s rather than a slice of bytes.

In [ ]:
 

Comments