Golang filesystem: Difference between revisions
From wikinotes
(Created page with " = Errors = <blockquote> <syntaxhighlight lang="go"> require "errors" require "os" require "io/fs" _, err := os.Stat("foo.bar") if errors.Is(err, fs.ErrExist) {...} if errors.Is(err, fs.ErrNotExist) {...} if errors.Is(err, fs.ErrPermission) {...} if errors.Is(err, fs.ErrPermission) {...} </syntaxhighlight> {{ expand | The old method defines <code>Is${N}</code> functions to test for various <code>os</code> package errors. | <syntaxhighlight lang="go"> require "os" //...") |
No edit summary |
||
Line 68: | Line 68: | ||
err := os.RemoveAll("/var/tmp/foo") | err := os.RemoveAll("/var/tmp/foo") | ||
err := os.Rename("a", "b") | err := os.Rename("a", "b") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Common Operations --> | </blockquote><!-- Common Operations --> | ||
Line 86: | Line 77: | ||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
File.Create | File.Create | ||
File.NewFile | File.NewFile | ||
File.Open | File.Open | ||
Line 97: | Line 87: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Files --> | </blockquote><!-- Files --> | ||
= Temporary Files/Directories = | |||
<blockquote> | |||
Temporary Directories | |||
<syntaxhighlight lang="go"> | |||
// creates tempdir, | |||
// '*' in filename is placement of random str | |||
// you must delete! | |||
path, err := os.MkdirTemp("/var/tmp", "cache-*") | |||
defer os.RemoveAll(path) | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="go"> | |||
File.CreateTemp | |||
</syntaxhighlight> | |||
</blockquote><!-- Temporary Files/Directories --> |
Revision as of 16:17, 18 June 2022
Errors
require "errors" require "os" require "io/fs" _, err := os.Stat("foo.bar") if errors.Is(err, fs.ErrExist) {...} if errors.Is(err, fs.ErrNotExist) {...} if errors.Is(err, fs.ErrPermission) {...} if errors.Is(err, fs.ErrPermission) {...}The old method defines
Is${N}
functions to test for variousos
package errors.
require "os" // example filesystem operations that create errors info, err := os.Stat("foo.bar") fd, err := os.Create("/etc/foo") // example error checks if os.IsExist(err) {...} if os.IsNotExist(err) {...} if os.IsPermission(err) {...}
Locations
// XDG spec dir, err := os.UserHomeDir() dir, err := os.UserConfigDir() dir, err := os.UserCacheDir()
Common Operations
import "os" import "errors" // check if file exists if _, err := os.Stat("/var/foo.txt"); errors.Is(err, fs.ErrNotExist) { fmt.Println("file does not exist!") } dir, err := os.Getwd() err := os.Chmod("foo.txt", 0644) err := os.Chown("foo.txt", -1, -1) // '-1' means do not change uid/gid err := os.Chown("foo.txt", 1000, -1) // set uid '1000' as owner err := os.Link("/var/foo", "/var/bar") // hardlink err := os.Symlink("/var/foo", "/var/bar") // symlink err := os.Mkdir("/var/foo") // create directory (non recursively) err := os.MkdirAll("/var/foo/bar/baz") // creates directory (recursively) err := os.Remove("/var/tmp/foo.bar") err := os.RemoveAll("/var/tmp/foo") err := os.Rename("a", "b")
Files
For more details, see golang input/output
File.Create File.NewFile File.Open File.Write File.WriteString File.Stat File.FileInfo
Temporary Files/Directories
Temporary Directories
// creates tempdir, // '*' in filename is placement of random str // you must delete! path, err := os.MkdirTemp("/var/tmp", "cache-*") defer os.RemoveAll(path)File.CreateTemp