Go Package os: Difference between revisions
Jump to navigation
Jump to search
Line 9: | Line 9: | ||
=Reading and Writing an Entire File in/from Memory= | =Reading and Writing an Entire File in/from Memory= | ||
To read the content of an entire file into a byte slice: | |||
<syntaxhighlight lang='go'> | |||
bs, err := os.ReadFile("/Users/ovidiu/tmp/test.txt") | |||
</syntaxhighlight> | |||
To write in-memory data into a file: | |||
<syntaxhighlight lang='go'> | |||
s := "This\nis multi-line\nfile\ncontent\n" | |||
err := os.WriteFile("/Users/ovidiu/tmp/test2.txt", []byte(s), 0777) | |||
</syntaxhighlight> | |||
<code>os.ReadFile</code> and <code>os.WriteFile</code> close the file after use. | |||
=TO DEPLETE= | =TO DEPLETE= |
Revision as of 18:25, 25 August 2023
External
Internal
Overview
Reading and Writing an Entire File in/from Memory
To read the content of an entire file into a byte slice:
bs, err := os.ReadFile("/Users/ovidiu/tmp/test.txt")
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.ReadFile
and os.WriteFile
close the file after use.
TO DEPLETE
External
Internal
Functions
os.Open()
Works with files and directories.
os.Create()
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: