Golang (go language) standard Library analysis OS (5)

Source: Internet
Author: User
Tags symlink
This is a creation in Article, where the information may have evolved or changed.

Golang Standard Library


Today we continue OS package analysis ~ ~ ~ Wood has nonsense ~ ~
(1) OS. Rename () The prototype of this function is the Func Rename (oldname, newname string) error, the input is the old file name, the new filename, and then returns an error in fact the function of the actual implementation of the Syscall. Rename () is then renamed by MoveFile (from *uint16, to *uint16) (err error) = Movefilew We'll tell you when we talk about Syscall.
[PHP]
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")
}
}
}
[/php]
(2) OS. The function of Samefile () 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,
[PHP]
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")
}
[/php]
(3) OS. Setenv () This function is very simple to set environment variables, function prototype func Setenv (key, value string) error input corresponding Key-value string, return error information
[PHP]
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 to D:/golang
}
[/php]
(4) OS. Symlink () for this function I can only say that does not support Windows platform, create soft connect func Symlink (oldname, newname string) error
[PHP]
Import (
"FMT"
"OS"
)

Func Main () {
ERR: = OS. Symlink ("1.go", "21.go")//does not support Windows platform support only Linux and Unix
Fmt. PRINTLN (ERR)
}
[/php]
(5) OS. TempDir () This function is very simple, return your local System temp directory, function prototype func tempdir () string, hey, do a comparison don't mess up
[PHP]
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 c:\users\admini~1\appdata\local\temp
}
[/php]
(6) OS. Truncate () Change the file's F. Size () This changes the length of the file content, function prototype func Truncate (name string, size Int64) error, remember Kazakhstan second is int64
[PHP]
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 now the 10 file has become a package ma
}
[/php]
Officially open tomorrow. Type File struct{}

Without permission, do not reprint this site any article: Micro network»golang (go Language) standard library analysis of the OS (5)

Related Article

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.