In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Golang的os标准库中常用函数的示例分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
os.Rename()这个函数的原型是func Rename(oldname, newname string) error,输入的是旧文件名,新文件名,然后返回一个error其实这个函数的真正实现用的syscall.Rename()然后通过MoveFile(from *uint16, to *uint16) (err error) = MoveFileW来重新命名
复制代码 代码如下:
import (
"fmt"
"os"
)
func main() {
err := os.Rename("1.go", "2.go")
if err != nil {
if os.IsExist(err) { //判断一个是否文件已经存在的错误
fmt.Println("文件已经存在")
os.Rename("1.go", "widuu_1.go")
}
}
}
os.SameFile()这个函数的作用是检测文件的信息是否相同所谓文件信息指的是os.Stat(),函数原型是func SameFile(fi1, fi2 FileInfo) bool
举个例子
复制代码 代码如下:
import (
"fmt"
"os"
)
func main() {
f1, _ := os.Stat("1.go")
f2, _ := os.Stat("21.go")
if os.SameFile(f1, f2) {
fmt.Println("两个文件一样")
return
}
fmt.Println("两个文件不一样")
}
os.Setenv()这个函数是设置环境变量的很简单,函数原型func Setenv(key, value string) error输入对应的key-value字符串,返回error信息
复制代码 代码如下:
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) //返回的是D:/golang
}
os.Symlink()对于这个函数我只能说不支持windows平台的,创建软连接func Symlink(oldname, newname string) error
复制代码 代码如下:
import (
"fmt"
"os"
)
func main() {
err := os.Symlink("1.go", "21.go") //不支持windows平台只支持linux和unix
fmt.Println(err)
}
os.TempDir()这个函数很简单,返回你本地的系统temp目录,函数原型func TempDir() string,嘿嘿,做个对比别乱了
复制代码 代码如下:
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
//创建临时的tmp
dir, _ := os.Getwd()
path, _ := ioutil.TempDir(dir, "tmp")
fmt.Println(path) //D:\test\tmp764030415
//这个返回的是系统temp
temp := os.TempDir()
fmt.Println(temp) //windows来说C:\Users\ADMINI~1\AppData\Local\Temp
}
os.Truncate()改变文件的f.Size()这个就改变了文件内容的长度了,函数原型func Truncate(name string, size int64) error,记得哈第二个是int64
复制代码 代码如下:
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现在是10 文件也变成了package ma
}
os.Create()这个函数是创见一个文件,函数的原型是func Create(name string) (file *File, err error)输入的是名称字符串类型,返回的是一个File的指针和一个error
复制代码 代码如下:
import (
"fmt"
"os"
"reflect"
)
func main() {
f, _ := os.Create("widuu_2.go")
defer f.Close()
fmt.Println(reflect.ValueOf(f).Type()) //*os.File
}
这个函数的原理其实是这样的OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666) O_RDWR也就是说用读写的权限,O_CREATE然后文件存在忽略,不存在创建它,O_TRUNC文件存在截取长度为0,这就解释了为什么我们明明有这个文件,我擦,创建之后哭了~啥都没有了~~用的时候需谨慎,先判断文件是否存在~
os.OpenFile函数的原型是func OpenFile(name string, flag int, perm FileMode) (file *File, err error)要指定文件权限和打开的方式,就是我们上边所用到的
复制代码 代码如下:
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())
}
这个就是上边的Create()只不过权限是0777以及下边的操作等大部分用到OpenFile()
os.Open()这个函数是打开文件使用的,函数原型是func Open(name string) (file *File, err error),返回值就不说了一样的,它的其实原理是这样的OpenFile(name, O_RDONLY, 0)以读文件的模式打开
复制代码 代码如下:
import (
"fmt"
"os"
"reflect"
)
func main() {
f, _ := os.Open("1.go")
defer f.Close()
}
os.Stat()这个是获取fileinfo的结构描述func Stat(name string) (fi FileInfo, err error)返回了Fileinfo这个结构,我们再前边也详细讲了
,其实它是怎么实现的呢?因为我们没讲syscall所以我们就讲一个,譬如FileInfo底层获取fs := &fileStat{name: basename(name)}然后后边逻辑大家可以看源代码
复制代码 代码如下:
import (
"fmt"
"os"
)
func main() {
f, _ := os.Stat("1.go")
fmt.Println(f.Size())
}
os.Fd()返回文件的句柄,函数原型是func (file *File) Fd() uintptr函数是这样的uintptr(file.fd) 返回的是文件的句柄,句柄是什么?句柄,是整个windows编程的基础。一个句柄是指使用的一个唯一的整数
复制代码 代码如下:
import (
"fmt"
"os"
)
func main() {
f, _ := os.Open("1.go")
fmt.Println(f.Fd()) //我的平台句柄是228
}
os.Pipe()这个函数获取的函数的读写指针,函数原型func Pipe() (r *File, w *File, err error)
复制代码 代码如下:
import (
"fmt"
"os"
)
func main() {
r, w, _ := os.Pipe()
fmt.Println(r, w) //&{0xc08402e120} &{0xc08402e180}
}
os.NewFile()函数原型是func NewFile(fd uintptr, name string) *File 第一个传入的是句柄,然后是文件名称,这个函数并不是真的创建了一个文件,是新建一个文件不保存,然后返回文件的指针
复制代码 代码如下:
import (
"fmt"
"os"
)
func main() {
f, _ := os.Open("ini.go")
defer f.Close()
f1 := os.NewFile(f.Fd(), "ceshi.go") //输如ini.go的句柄
defer f1.Close()
fd, _ := f1.Stat()
fmt.Println(fd.ModTime()) //返回的是ini.go的创建时间2013-11-27 09:11:50.2793737 +0800 CST
}
(f *File).Chdir()修改工作目录,函数原型func (f *File) Chdir() error,这个时候f必须是目录了,但是吧这个不支持windows
复制代码 代码如下:
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)
}
关于Golang的os标准库中常用函数的示例分析问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.