Golang of common functions in the OS standard library _golang

Source: Internet
Author: User
Tags function prototype symlink file permissions

Os. Rename () The prototype of this function is Func Rename (oldname, newname string) error, the input is the old filename, the new file name, and then returns an error in fact the function of the real implementation of the Syscall. Rename () and then renamed by MoveFile (from *uint16, to *uint16) (err error) = Movefilew

Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
ERR: = OS. Rename ("1.go", "2.go")
If Err!= nil {
If OS. Isexist (ERR) {//To determine if a file already exists error
Fmt. Println ("File already exists")
Os. Rename ("1.go", "Widuu_1.go")
}
}
}


Os. Samefile () The function is to detect whether the file information is the same as the so-called file information refers to the OS. Stat (), function prototype is func samefile (fi1, fi2 FileInfo) bool
As an example
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
F1, _: = OS. Stat ("1.go")
F2, _: = OS. Stat ("21.go")
If OS. Samefile (f1, F2) {
Fmt. Println ("Two Files")
Return
}
Fmt. Println ("Two files are different")
}


Os. Setenv () This function is simple to set environment variables, function prototype func setenv (key, value string) error input corresponding Key-value string, return error information
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
ERR: = OS. Setenv ("Wd_path", "D:/golang")
If Err!= nil {
Fmt. PRINTLN (ERR)
}
ENV: = OS. Getenv ("Wd_path")
Fmt. PRINTLN (env)//return is D:/golang
}


Os. Symlink () for this function I can only say that the Windows platform does not support the creation of soft connections func Symlink (oldname, newname string) error
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
ERR: = OS. Symlink ("1.go", "21.go")//unsupported Windows platform supports Linux and UNIX only
Fmt. PRINTLN (ERR)
}


Os. TempDir () This function is very simple, return to your local system temp directory, function prototype func tempdir () string, hehe, make a comparison don't mess up
Copy Code code as follows:

Import (
"FMT"
"Io/ioutil"
"OS"
)

Func Main () {
Create a temporary TMP
Dir, _: = OS. GETWD ()
Path, _: = Ioutil. TempDir (dir, "tmp")
Fmt. Println (PATH)//d:\test\tmp764030415
This returns the system temp.
Temp: = OS. TempDir ()
Fmt. Println (temp)//windows for c:\users\admini~1\appdata\local\temp
}


Os. Truncate () Changes the file's F. Size () This changes the length of the contents of the file, the function prototype func Truncate (name string, size Int64) error, remember Kazakhstan the second is int64
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
F, _: = OS. Stat ("1.go")
Fmt. Println (F.size ())//1.go 83
ERR: = OS. Truncate ("1.go", 10)
If Err!= nil {
Fmt. PRINTLN (ERR)
}
F, _ = OS. Stat ("1.go")
Fmt. Println (F.size ())//1.go is now 10 files and has become package Ma.
}


Os. Create () This function is a file, the prototype of the function is func Create (name string) (file *file, err Error) input is the name string type, return is a file pointer and an error
Copy Code code as follows:

Import (
"FMT"
"OS"
"Reflect"
)

Func Main () {
F, _: = OS. Create ("Widuu_2.go")
Defer F.close ()
Fmt. Println (reflect. ValueOf (f). Type ())//*os. File
}


The principle of this function is actually this openfile (name, o_rdwr| o_create| O_trunc, 0666) O_rdwr that is, the right to read and write, o_create and then the file exists ignored, does not exist to create it, O_trunc file existence interception length of 0, which explains why we obviously have this file, I wipe, after the creation of crying ~ Nothing ~ ~ Use the time need to be cautious, first determine whether the file exists ~

Os. The prototype of the OpenFile function is the func openfile (name string, flag int, perm FileMode) (file *file, err Error) to specify file permissions and how to open it, which is what we use above

Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
F, _: = OS. OpenFile ("10.go", OS. O_rdwr|os. O_create|os. O_trunc, 0777)
Defer F.close ()
Fmt. Println (F.stat ())
}


This is the top of the Create () only the permission is 0777 and the operation of the bottom most of the use of OpenFile ()

Os. Open () This function is opened using the file, the function prototype is func open (name string) (file *file, err error), the return value does not say the same, it is actually the principle of such openfile (name, o_rdonly, 0 Opens in a read-file mode

Copy Code code as follows:

Import (
"FMT"
"OS"
"Reflect"
)

Func Main () {
F, _: = OS. Open ("1.go")
Defer F.close ()
}


Os. Stat () This is the structure description that gets the FileInfo func Stat (name string) (Fi FileInfo, err error) returns FILEINFO This structure, and we'll talk about it in the front.
, in fact, how it is achieved? Because we didn't talk about Syscall, so we're going to say one, like FileInfo. Get fs: = &filestat{name:basename (name)} then you can see the source code behind the logic
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
F, _: = OS. Stat ("1.go")
Fmt. Println (F.size ())
}


Os. FD () returns the handle to the file, which is the func (file *file) Fd () UIntPtr function is such that uintptr (FILE.FD) returns the handle to the file, and what is the handle? Handle, which is the foundation of entire Windows programming. A handle refers to a unique integer that is used
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
F, _: = OS. Open ("1.go")
Fmt. Println (F.FD ())//My platform handle is 228
}


Os. Pipe () This function gets the read-write pointer to the function, the function prototype Func Pipe () (R *file, W *file, err Error)
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
R, W, _: = OS. Pipe ()
Fmt. Println (R, W)//&{0xc08402e120} &{0xc08402e180}
}


Os. The NewFile () function prototype is func NewFile (fd uintptr, name string) *file the first incoming is a handle, then the file name, which is not really creating a file, is a new file is not saved, and then returns a pointer to the file
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
F, _: = OS. Open ("Ini.go")
Defer F.close ()
F1: = OS. NewFile (F.FD (), "ceshi.go")/the handle of the output as Ini.go
Defer F1. Close ()
FD, _: = F1. Stat ()
Fmt. Println (FD. Modtime ())//Returns the creation time of Ini.go 2013-11-27 09:11:50.2793737 +0800 CST

}


(f *file). Chdir () Modify working directory, function prototype func (f *file) Chdir () error, this time F must be directory, but this does not support windows
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
Dir, _: = OS. GETWD ()
Fmt. Println (dir)
F, _: = OS. Open ("views")
ERR: = F.chdir ()
If Err!= nil {
Fmt. PRINTLN (ERR)
}
Dir1, _: = OS. GETWD ()
Fmt. Println (DIR1)
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.