The `os` package in Go
The os
package in Go
The os
package in Go provides a wide range of functions for interacting with the operating system. It includes functions for working with files, directories, environment variables, and more. Here’s an overview of some of the most commonly used functions in the os
package:
Function | Description |
---|---|
os.Open(name string) | Opens a file for reading and returns a file descriptor and an error if the file cannot be opened. |
os.Create(name string) | Creates a new file or truncates an existing file to zero length and returns a file descriptor and error. |
os.Remove(name string) | Deletes a file or directory and returns an error if it cannot be removed. |
os.RemoveAll(path string) | Removes a path and any children it contains recursively. |
os.Mkdir(name string, perm os.FileMode) | Creates a directory with the specified name and permission mode. |
os.MkdirAll(path string, perm os.FileMode) | Creates a directory and any parent directories that do not exist. |
os.Getwd() | Returns the current working directory. |
os.Chdir(dir string) | Changes the current working directory to the specified directory. |
os.Exit(code int) | Exits the program with a specified status code. |
os.Getenv(key string) | Retrieves the value of the environment variable for the given key. |
os.Setenv(key, value string) | Sets the value of the environment variable for the given key. |
os.Environ() | Returns a slice of strings representing all the environment variables in the format KEY=value . |
os.Stat(name string) | Returns information about a file or directory, or an error if it does not exist. |
os.Symlink(oldname, newname string) | Creates a symbolic link that points to a specified file or directory. |
os.UserHomeDir() | Returns the home directory of the current user. |
os.Hostname() | Returns the hostname of the machine. |
This format provides a clear overview of each function along with its description.