ADVERTISEMENT
ADVERTISEMENT

Golang os Package

Golang | os Package: Here, we are going to learn about the Golang os package, its constants, variables, and functions with examples.

In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The error handling is Go-like; failing calls return values of type error rather than error numbers. Often, more information is available within the error. The os interface is intended to be uniform across all operating systems. Features not generally available appear in the system-specific package syscall.

To use these os package constants, variables, and functions – we need to import the os package.

os Package import statement:

import "os"

List of Golang os Package Constants, Variables, and Functions

The following are the os package constants, variables, and functions,

Constants

Constants Description Value
os.O_RDONLY It is used to specify the read-only mode to open a file in the read-only mode. 0
os.O_WRONLY It is used to specify the write-only mode to open a file in the write-only mode. 1
os.O_RDWR It is used to specify the read-write mode to open a file in the read-write mode. 2
os.O_APPEND It is used to append data to the file when writing i.e., enables to append data to the file. 1024
os.O_CREATE It is used to create a file if none exists. 64
os.O_EXCL It is used to create a file if the file does not exist. It is used with the O_CREATE constant. Please note that the file must not exist. 128
os.SEEK_SET It is used to seek relative to the origin of the file i.e., it specifies the starting byte of the file pointer, it is used with the Seek() function to seek the file pointer at a given position from the starting position of the file pointer. The value of SEEK_SET constant 0. 0
os.SEEK_CUR It is used to seek relative to the current offset i.e., it specifies the current position of the file pointer, it is used with the Seek() function to seek the file pointer at the given position from the current position of the file pointer. The value of SEEK_CUR constant 1. 1
os.SEEK_END It is used to seek relative to the end i.e., it specifies the end position of the file pointer, it is used with the Seek() function to seek the file pointer at the given position from the end position of the file pointer. The value of SEEK_END constant 2. 2
os.PathSeparator It is used to get the operating specific path separator. The value of the PathSeparator constant may be different based on the operating system. '/' (on Unix-based OS)
os.PathListSeparator It is used to get the operating-specific path list separator. The value of the PathListSeparator constant may be different based on the operating system. ':' (on Unix-based OS)
os Package FileMode Constants These single letters are the abbreviations used by the String method's formatting. -

Functions

Functions Description
os.Chdir() It is used to change the current working directory to the named directory. If there is an error, it will be of type *PathError.
os.Chmod() It is used to change the mode of the named file to mode. If the file is a symbolic link, it changes the mode of the link's target. If there is an error, it will be of type *PathError.
os.Chown() It is used to change the numeric uid and gid of the named file. If the file is a symbolic link, it changes the uid and gid of the link's target. A uid or gid of -1 means to not change that value. If there is an error, it will be of type *PathError.
os.Chtimes() It is used to change the access and modification times of the named file, similar to the Unix utime() or utimes() functions.
os.Clearenv() It is used to delete all environment variables.
os.DirFS() It is used to get the file system (an fs.FS) for the tree of files rooted at the directory dir.
os.Environ() It is used to get a copy of strings representing the environment, in the form "key=value".
os.Executable() It is used to get the path name for the executable that started the current process.
os.Exit() It is used to exit from the current program with the given status code.
os.Expand() It is used to replace ${var} or $var in the string based on the mapping function. For example, os.ExpandEnv(s) is equivalent to os.Expand(s, os.Getenv).
os.ExpandEnv() It is used to replace ${var} or $var in the string according to the values of the current environment variables, undefined variables are replaced by the empty string.
os.Getegid() It is used to get the numeric effective group id of the caller.
os.Getenv() It is used to get the value of the environment variable specified by the key. It returns the value, which will be empty if the variable is not present.
os.Geteuid() It is used to get the numeric effective user id of the caller.
os.Getgid() It is used to get the numeric effective group id of the caller.
os.Getgroups() It is used to get the list of the numeric ids of groups that the caller belongs to.
os.Getpagesize() It is used to get the underlying system's memory page size.
os.Getpid() It is used to get the process id of the caller.
os.Getppid() It is used to get the process id of the caller's parent.
os.Getuid() It is used to get the numeric user id of the caller.
os.Getwd() is used to get the rooted path name corresponding to the current directory. If the current directory can be reached via multiple paths (due to symbolic links), Getwd() function may return any one of them.
os.Hostname() It is used to get the hostname reported by the kernel.
os.IsExist() It is used to check whether the given error is known to report that a file or directory already exists. The IsExist() function is satisfied by ErrExist as well as some syscall errors. This function predates errors.Is.
os.IsNotExist() It is used to check whether the given error is known to report that a file or directory does not exist. The IsNotExist() function is satisfied by ErrExist as well as some syscall errors. This function predates errors.Is.
os.IsPathSeparator() It is used to check whether the given character (c) is a directory separator character or not.
os.IsPermission() It is used to check whether the given error is known to report that permission is denied. The IsPermission() function is satisfied by ErrPermission as well as some syscall errors. This function predates errors.Is.
os.IsTimeout() It is used to check whether the given error is known to report that a timeout occurred. This function predates errors.Is.
ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.