Go Package os: Difference between revisions
Line 50: | Line 50: | ||
===<tt>os.Create()</tt>=== | ===<tt>os.Create()</tt>=== | ||
{{External|https://golang.org/pkg/os/#Create}} | {{External|https://golang.org/pkg/os/#Create}} | ||
Open the file with <code>os.Create()</code>. | Open the file with <code>os.Create()</code>. It will return a <code>os.File</code> that implements <code>[[Go_Package_io#Reader|io.Reader]]</code>, <code>[[Go_Package_io#Writer|io.Writer]]</code> and <code>[[Go_Package_io#Closer|io.Closer]]</code> interfaces. | ||
Then use the <code>File</code> <code>Write()</code> method write the content of a byte slice, or <code>WriteString()</code> to write strings. The file must be explicitly closed after use with <code>Close()</code>: | Then use the <code>File</code> <code>Write()</code> method write the content of a byte slice, or <code>WriteString()</code> to write strings. The file must be explicitly closed after use with <code>Close()</code>: | ||
Line 80: | Line 80: | ||
<font color=darkkhaki>What happens if open the file with <code>os.Open()</code> and then I write?</font> | <font color=darkkhaki>What happens if open the file with <code>os.Open()</code> and then I write?</font> | ||
==Close== | ==Close== | ||
Revision as of 00:05, 19 December 2023
External
Internal
Overview
Reading and Writing an Entire File in/from Memory
Read with ReadFile()
To read the content of an entire file into a byte slice:
bs, err := os.ReadFile("/Users/ovidiu/tmp/test.txt")
os.ReadFile()
closes the file after use.
Write with WriteFile()
To write in-memory data into a file:
s := "This\nis multi-line\nfile\ncontent\n"
err := os.WriteFile("/Users/ovidiu/tmp/test2.txt", []byte(s), 0777)
os.WriteFile()
closes the file after use.
Sequentially Reading and Writing Byte Arrays
Reading
Open the file with os.Open()
and then use the File
Read()
method to read into a byte slice.
bs := make([]byte, 10)
f, err := os.Open("/Users/ovidiu/tmp/test.txt")
if err != nil {
// ...
return
}
defer f.Close()
for {
cnt, err := f.Read(bs)
if err != nil {
fmt.Println(err)
f.Close()
return
}
fmt.Println("read ", cnt, " bytes")
}
The file must be explicitly closed after use with Close()
.
Writing
os.Create()
Open the file with os.Create()
. It will return a os.File
that implements io.Reader
, io.Writer
and io.Closer
interfaces.
Then use the File
Write()
method write the content of a byte slice, or WriteString()
to write strings. The file must be explicitly closed after use with Close()
:
var err error
f, err := os.Create("/Users/ovidiu/tmp/test3.txt")
if err != nil {
// ...
}
_, err = f.WriteString("This is the first line\n")
if err != nil {
// ...
}
_, err = f.WriteString("This is the second line\n")
if err != nil {
// ...
}
err = f.Close()
if err != nil {
// ...
}
The file content will be:
This is the first line
This is the second line
What happens if open the file with os.Open()
and then I write?
Close
TO DEPLETE
Functions
os.Open()
Works with files and directories.
os.File
os.File Methods
FileInfo
FileInfo Methods
- Size()
os.Args
os.Args is a string slice that holds the command-line arguments, starting with the program name. For more details on how to use see: