{"id":52534,"date":"2025-08-11T15:39:05","date_gmt":"2025-08-11T07:39:05","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/52534.html"},"modified":"2025-08-11T15:39:05","modified_gmt":"2025-08-11T07:39:05","slug":"go%e8%af%ad%e8%a8%80%e4%b8%ados%e6%96%87%e4%bb%b6%e7%b1%bb%ef%bc%8c%e6%8c%89%e7%b1%bb%e5%88%ab%e6%95%b4%e7%90%86%e6%a0%b8%e5%bf%83%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/52534.html","title":{"rendered":"go\u8bed\u8a00\u4e2dos\u6587\u4ef6\u7c7b\uff0c\u6309\u7c7b\u522b\u6574\u7406\u6838\u5fc3\u64cd\u4f5c"},"content":{"rendered":"<h2>\u4e00.\u57fa\u7840\u6587\u4ef6\u64cd\u4f5c&#xff1a;<\/h2>\n<p>1.\u6587\u4ef6\u6253\u5f00\u4e0e\u521b\u5efa<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:643px\">\n<tbody>\n<tr>\n<td style=\"text-align:center;width:84px\">\u51fd\u6570<\/td>\n<td style=\"text-align:center;width:123px\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:416px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:84px\">os.Create()<\/td>\n<td style=\"text-align:center;width:123px\">\u521b\u5efa\u6216\u622a\u65ad\u6587\u4ef6<\/td>\n<td style=\"text-align:center;width:416px\">f, err :&#061; os.Create(&#034;file.txt&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:84px\">os.Open()<\/td>\n<td style=\"text-align:center;width:123px\">\u53ea\u8bfb\u65b9\u5f0f\u6253\u5f00<\/td>\n<td style=\"text-align:center;width:416px\">f, err :&#061; os.Open(&#034;file.txt&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:84px\">os.OpenFile()<\/td>\n<td style=\"text-align:center;width:123px\">\u81ea\u5b9a\u4e49\u6a21\u5f0f\u6253\u5f00<\/td>\n<td style=\"text-align:center;width:416px\">f, err :&#061; os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0755)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6587\u4ef6\u6253\u5f00\u6a21\u5f0f\u5e38\u91cf&#xff1a;<\/p>\n<p>os.O_RDONLY  \/\/ \u53ea\u8bfb<br \/>\nos.O_WRONLY  \/\/ \u53ea\u5199<br \/>\nos.O_RDWR    \/\/ \u8bfb\u5199<br \/>\nos.O_APPEND  \/\/ \u8ffd\u52a0<br \/>\nos.O_CREATE  \/\/ \u4e0d\u5b58\u5728\u5219\u521b\u5efa<br \/>\nos.O_EXCL    \/\/ \u4e0eO_CREATE\u8fde\u7528&#xff0c;\u6587\u4ef6\u5fc5\u987b\u4e0d\u5b58\u5728<br \/>\nos.O_SYNC    \/\/ \u540c\u6b65I\/O<br \/>\nos.O_TRUNC   \/\/ \u6253\u5f00\u65f6\u6e05\u7a7a\u6587\u4ef6<\/p>\n<p>2.\u6587\u4ef6\u8bfb\u5199<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:618px\">\n<tbody>\n<tr>\n<td style=\"text-align:center\">\u65b9\u6cd5<\/td>\n<td style=\"text-align:center\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:231px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">(*File).Read()<\/td>\n<td style=\"text-align:center\">\u8bfb\u53d6\u5230\u5b57\u8282\u5207\u7247<\/td>\n<td style=\"text-align:center;width:231px\">n, err :&#061; f.Read(buf)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">(*File).ReadAt()<\/td>\n<td style=\"text-align:center\">\u6307\u5b9a\u4f4d\u7f6e\u8bfb\u53d6<\/td>\n<td style=\"text-align:center;width:231px\">n, err :&#061; f.ReadAt(buf, offset)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">(*File).Write()<\/td>\n<td style=\"text-align:center\">\u5199\u5165\u5b57\u8282\u5207\u7247<\/td>\n<td style=\"text-align:center;width:231px\">\u00a0n, err :&#061; f.Write([]byte(&#034;data&#034;))<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">(*File).WriteString()<\/td>\n<td style=\"text-align:center\">\u5199\u5165\u5b57\u7b26\u4e32<\/td>\n<td style=\"text-align:center;width:231px\">n, err :&#061; f.WriteString(&#034;text&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">(*File).WriteAt()\u00a0<\/td>\n<td style=\"text-align:center\">\u6307\u5b9a\u4f4d\u7f6e\u5199\u5165<\/td>\n<td style=\"text-align:center;width:231px\">n, err :&#061; f.WriteAt(data, offset)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>3.\u6587\u4ef6\u5b9a\u4f4d<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:545px\">\n<tbody>\n<tr>\n<td style=\"text-align:center\">\u65b9\u6cd5<\/td>\n<td style=\"text-align:center\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:272px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">(*File).Seek()<\/td>\n<td style=\"text-align:center\">\u79fb\u52a8\u8bfb\u5199\u6307\u9488<\/td>\n<td style=\"text-align:center;width:272px\">pos, err :&#061; f.Seek(10, io.SeekStart)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6587\u4ef6\u6253\u5f00\u6a21\u5f0f\u5e38\u91cf&#xff1a;\u4f8b\u5982&#xff1a;<\/p>\n<p>io.SeekStart   \/\/ \u6587\u4ef6\u5f00\u5934<br \/>\nio.SeekCurrent \/\/ \u5f53\u524d\u4f4d\u7f6e<br \/>\nio.SeekEnd     \/\/ \u6587\u4ef6\u672b\u5c3e<\/p>\n<p>4.\u6587\u4ef6\u5173\u95ed\u4e0e\u540c\u6b65<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:483px\">\n<tbody>\n<tr>\n<td style=\"text-align:center;width:154px\">\u65b9\u6cd5<\/td>\n<td style=\"text-align:center;width:120px\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:206px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:154px\">(*File).Close()<\/td>\n<td style=\"text-align:center;width:120px\">\u5173\u95ed\u6587\u4ef6<\/td>\n<td style=\"text-align:center;width:206px\">err :&#061; f.Close()<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:154px\">(*File).Sync()<\/td>\n<td style=\"text-align:center;width:120px\">\u5237\u76d8\u540c\u6b65<\/td>\n<td style=\"text-align:center;width:206px\">err :&#061; f.Sync()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4e8c.\u6587\u4ef6\u4fe1\u606f\u4e0e\u5c5e\u6027&#xff1a;<\/h2>\n<p>1.\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:583px\">\n<tbody>\n<tr>\n<td style=\"text-align:center\">\u51fd\u6570\/\u65b9\u6cd5<\/td>\n<td style=\"text-align:center\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:245px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">os.Stat()<\/td>\n<td style=\"text-align:center\">\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f<\/td>\n<td style=\"text-align:center;width:245px\">fi, err :&#061; os.Stat(&#034;file.txt&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">os.Lstat()<\/td>\n<td style=\"text-align:center\">\u540cStat\u4f46\u4e0d\u89e3\u6790\u94fe\u63a5<\/td>\n<td style=\"text-align:center;width:245px\">fi, err :&#061; os.Lstat(&#034;symlink&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">(*File).Stat()<\/td>\n<td style=\"text-align:center\">\u83b7\u53d6\u5df2\u6253\u5f00\u6587\u4ef6\u4fe1\u606f<\/td>\n<td style=\"text-align:center;width:245px\">fi, err :&#061; f.Stat()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>FileInfo\u63a5\u53e3&#xff1a;<\/p>\n<p>type FileInfo interface {<br \/>\n    Name() string       \/\/ \u6587\u4ef6\u540d&#xff08;\u4e0d\u542b\u8def\u5f84&#xff09;<br \/>\n    Size() int64        \/\/ \u6587\u4ef6\u5927\u5c0f&#xff08;\u5b57\u8282&#xff09;<br \/>\n    Mode() FileMode     \/\/ \u6587\u4ef6\u6a21\u5f0f<br \/>\n    ModTime() time.Time \/\/ \u4fee\u6539\u65f6\u95f4<br \/>\n    IsDir() bool        \/\/ \u662f\u5426\u4e3a\u76ee\u5f55<br \/>\n    Sys() interface{}   \/\/ \u5e95\u5c42\u6570\u636e\u6e90<br \/>\n}<\/p>\n<p>2.\u6587\u4ef6\u5c5e\u6027\u64cd\u4f5c<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:580px\">\n<tbody>\n<tr>\n<td style=\"text-align:center;width:131px\">\u51fd\u6570\/\u65b9\u6cd5<\/td>\n<td style=\"text-align:center;width:124px\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:323px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:131px\">os.Chmod()<\/td>\n<td style=\"text-align:center;width:124px\">\u4fee\u6539\u6743\u9650<\/td>\n<td style=\"text-align:center;width:323px\">err :&#061; os.Chmod(&#034;file.txt&#034;, 0644)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:131px\">os.Chown()\u00a0<\/td>\n<td style=\"text-align:center;width:124px\">\u4fee\u6539\u6240\u6709\u8005<\/td>\n<td style=\"text-align:center;width:323px\">err :&#061; os.Chown(&#034;file.txt&#034;, uid, gid)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:131px\">os.Chtimes()\u00a0<\/td>\n<td style=\"text-align:center;width:124px\">\u4fee\u6539\u65f6\u95f4<\/td>\n<td style=\"text-align:center;width:323px\">err :&#061; os.Chtimes(&#034;file.txt&#034;, atime, mtime)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4e09.\u76ee\u5f55\u64cd\u4f5c<\/h2>\n<p>1.\u76ee\u5f55\u521b\u5efa\u4e0e\u5220\u9664<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:568px\">\n<tbody>\n<tr>\n<td style=\"text-align:center;width:144px\">\u51fd\u6570<\/td>\n<td style=\"text-align:center;width:168px\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:255px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:144px\">os.Mkdir()<\/td>\n<td style=\"text-align:center;width:168px\">\u521b\u5efa\u5355\u7ea7\u76ee\u5f55\u00a0<\/td>\n<td style=\"text-align:center;width:255px\">err :&#061; os.Mkdir(&#034;dir&#034;, 0755)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:144px\">os.MkdirAll()<\/td>\n<td style=\"text-align:center;width:168px\">\u9012\u5f52\u521b\u5efa\u76ee\u5f55<\/td>\n<td style=\"text-align:center;width:255px\">err :&#061; os.MkdirAll(&#034;a\/b\/c&#034;, 0755)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:144px\">os.Remove()<\/td>\n<td style=\"text-align:center;width:168px\">\u5220\u9664\u6587\u4ef6\u6216\u7a7a\u76ee\u5f55<\/td>\n<td style=\"text-align:center;width:255px\">err :&#061; os.Remove(&#034;empty_dir&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:144px\">os.RemoveAll()<\/td>\n<td style=\"text-align:center;width:168px\">\u9012\u5f52\u5220\u9664\u76ee\u5f55\u00a0<\/td>\n<td style=\"text-align:center;width:255px\">err :&#061; os.RemoveAll(&#034;dir&#034;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>2.\u76ee\u5f55\u904d\u5386<\/p>\n<table align=\"center\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:500px\">\n<tbody>\n<tr>\n<td style=\"text-align:center\">\u51fd\u6570<\/td>\n<td style=\"text-align:center\">\u529f\u80fd<\/td>\n<td style=\"text-align:center\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">os.ReadDir()<\/td>\n<td style=\"text-align:center\">\u8bfb\u53d6\u76ee\u5f55\u5185\u5bb9<\/td>\n<td style=\"text-align:center\">entries, err :&#061; os.ReadDir(&#034;.&#034;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>DirEntry\u63a5\u53e3&#xff1a;<\/p>\n<p>type DirEntry interface {<br \/>\n    Name() string<br \/>\n    IsDir() bool<br \/>\n    Type() FileMode<br \/>\n    Info() (FileInfo, error)<br \/>\n}<\/p>\n<p>2.\u83b7\u53d6\u5de5\u4f5c\u76ee\u5f55<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:500px\">\n<tbody>\n<tr>\n<td style=\"text-align:center\">\u51fd\u6570<\/td>\n<td style=\"text-align:center\">\u529f\u80fd<\/td>\n<td style=\"text-align:center\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">os.Getwd()<\/td>\n<td style=\"text-align:center\">\u83b7\u53d6\u5f53\u524d\u76ee\u5f55\u00a0<\/td>\n<td style=\"text-align:center\">wd, err :&#061; os.Getwd()<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">os.Chdir()\u00a0<\/td>\n<td style=\"text-align:center\">\u6539\u53d8\u5f53\u524d\u76ee\u5f55<\/td>\n<td style=\"text-align:center\">err :&#061; os.Chdir(&#034;\/tmp&#034;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u56db.\u8def\u5f84\u5904\u7406<\/h2>\n<p>\u6807\u51c6\u5e93path\/filepath<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:601px\">\n<tbody>\n<tr>\n<td style=\"text-align:center;width:148px\">\u51fd\u6570<\/td>\n<td style=\"text-align:center;width:159px\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:290px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">filepath.Abs()<\/td>\n<td style=\"text-align:center;width:159px\">\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\u00a0<\/td>\n<td style=\"text-align:center;width:290px\">abs, err :&#061; filepath.Abs(&#034;.&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">filepath.Join()<\/td>\n<td style=\"text-align:center;width:159px\">\u5b89\u5168\u62fc\u63a5\u8def\u5f84<\/td>\n<td style=\"text-align:center;width:290px\">p :&#061; filepath.Join(&#034;dir&#034;, &#034;file.txt&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">filepath.Split()<\/td>\n<td style=\"text-align:center;width:159px\">\u5206\u5272\u76ee\u5f55\/\u6587\u4ef6\u00a0<\/td>\n<td style=\"text-align:center;width:290px\">dir, file :&#061; filepath.Split(&#034;\/a\/b.txt&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">filepath.Dir()<\/td>\n<td style=\"text-align:center;width:159px\">\u83b7\u53d6\u76ee\u5f55\u90e8\u5206<\/td>\n<td style=\"text-align:center;width:290px\">dir :&#061; filepath.Dir(&#034;\/a\/b.txt&#034;) \/\/ &#034;\/a&#034;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">filepath.Base()<\/td>\n<td style=\"text-align:center;width:159px\">\u83b7\u53d6\u6587\u4ef6\u540d\u90e8\u5206\u00a0<\/td>\n<td style=\"text-align:center;width:290px\">file :&#061; filepath.Base(&#034;\/a\/b.txt&#034;) \/\/ &#034;b.txt&#034;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">filepath.Ext()<\/td>\n<td style=\"text-align:center;width:159px\">\u83b7\u53d6\u6269\u5c55\u540d<\/td>\n<td style=\"text-align:center;width:290px\">ext :&#061; filepath.Ext(&#034;a.txt&#034;) \/\/ &#034;.txt&#034;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">filepath.Glob()<\/td>\n<td style=\"text-align:center;width:159px\">\u6587\u4ef6\u901a\u914d\u7b26\u5339\u914d<\/td>\n<td style=\"text-align:center;width:290px\">matches, err :&#061; filepath.Glob(&#034;*.go&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">filepath.Walk()<\/td>\n<td style=\"text-align:center;width:159px\">\u9012\u5f52\u904d\u5386\u76ee\u5f55<\/td>\n<td style=\"text-align:center;width:290px\">err :&#061; filepath.Walk(&#034;.&#034;, walkFn)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">filepath.WalkDir()<\/td>\n<td style=\"text-align:center;width:159px\">\u66f4\u9ad8\u6548\u904d\u5386<\/td>\n<td style=\"text-align:center;width:290px\">err :&#061; filepath.WalkDir(&#034;.&#034;, walkFn)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\/\/ \u8def\u5f84\u5904\u7406<br \/>\nimport &#034;path\/filepath&#034;<\/p>\n<p>\/\/ \u62fc\u63a5\u8def\u5f84 (\u8de8\u5e73\u53f0\u517c\u5bb9)<br \/>\npath :&#061; filepath.Join(&#034;dir&#034;, &#034;subdir&#034;, &#034;file.txt&#034;) <\/p>\n<p>\/\/ \u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<br \/>\nabsPath, _ :&#061; filepath.Abs(&#034;relative\/path&#034;)<\/p>\n<p>\/\/ \u83b7\u53d6\u76ee\u5f55\u90e8\u5206<br \/>\ndir :&#061; filepath.Dir(&#034;\/path\/to\/file.txt&#034;) \/\/ &#034;\/path\/to&#034;<\/p>\n<p>\/\/ \u83b7\u53d6\u6587\u4ef6\u540d\u90e8\u5206<br \/>\nfile :&#061; filepath.Base(&#034;\/path\/to\/file.txt&#034;) \/\/ &#034;file.txt&#034;<\/p>\n<p>\/\/ \u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d<br \/>\next :&#061; filepath.Ext(&#034;file.txt&#034;) \/\/ &#034;.txt&#034;<\/p>\n<p>\/\/ \u8def\u5f84\u904d\u5386<br \/>\n\/\/ \u9012\u5f52\u904d\u5386\u76ee\u5f55<br \/>\nfilepath.Walk(&#034;root&#034;, func(path string, info os.FileInfo, err error) error {<br \/>\n    if !info.IsDir() {<br \/>\n        fmt.Println(&#034;File:&#034;, path)<br \/>\n    }<br \/>\n    return nil<br \/>\n})<\/p>\n<p>\/\/ \u6a21\u5f0f\u5339\u914d<br \/>\nmatches, _ :&#061; filepath.Glob(&#034;*.go&#034;) \/\/ \u6240\u6709Go\u6587\u4ef6<\/p>\n<h2>\u4e94.\u7279\u6b8a\u6587\u4ef6\u64cd\u4f5c<\/h2>\n<p>1.\u4e34\u65f6\u6587\u4ef6\/\u76ee\u5f55<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:601px\">\n<tbody>\n<tr>\n<td style=\"text-align:center;width:148px\">\u51fd\u6570<\/td>\n<td style=\"text-align:center;width:159px\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:290px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">os.CreateTemp()<\/td>\n<td style=\"text-align:center;width:159px\">\u521b\u5efa\u4e34\u65f6\u6587\u4ef6<\/td>\n<td style=\"text-align:center;width:290px\">f, err :&#061; os.CreateTemp(&#034;&#034;, &#034;prefix&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">os.MkdirTemp()<\/td>\n<td style=\"text-align:center;width:159px\">\u521b\u5efa\u4e34\u65f6\u76ee\u5f55\u00a0<\/td>\n<td style=\"text-align:center;width:290px\">dir, err :&#061; os.MkdirTemp(&#034;&#034;, &#034;prefix&#034;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>2.\u7b26\u53f7\u94fe\u63a5<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:601px\">\n<tbody>\n<tr>\n<td style=\"text-align:center;width:148px\">\u51fd\u6570<\/td>\n<td style=\"text-align:center;width:159px\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:290px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">os.Symlink()<\/td>\n<td style=\"text-align:center;width:159px\">\u521b\u5efa\u7b26\u53f7\u94fe\u63a5\u00a0<\/td>\n<td style=\"text-align:center;width:290px\">err :&#061; os.Symlink(&#034;target&#034;, &#034;link&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">os.Readlink()<\/td>\n<td style=\"text-align:center;width:159px\">\u8bfb\u53d6\u94fe\u63a5\u76ee\u6807<\/td>\n<td style=\"text-align:center;width:290px\">target, err :&#061; os.Readlink(&#034;link&#034;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>3.\u786c\u94fe\u63a5<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:601px\">\n<tbody>\n<tr>\n<td style=\"text-align:center;width:148px\">\u51fd\u6570<\/td>\n<td style=\"text-align:center;width:159px\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:290px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">os.Link()<\/td>\n<td style=\"text-align:center;width:159px\">\u521b\u5efa\u786c\u94fe\u63a5\u00a0<\/td>\n<td style=\"text-align:center;width:290px\">err :&#061; os.Link(&#034;old&#034;, &#034;new&#034;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u516d.\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c<\/h2>\n<p>1.\u6587\u4ef6\u9501<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:616px\">\n<tbody>\n<tr>\n<td style=\"text-align:center;width:119px\">\u51fd\u6570<\/td>\n<td style=\"text-align:center;width:161px\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:332px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:119px\">(*File).Fd()<\/td>\n<td style=\"text-align:center;width:161px\">\u83b7\u53d6\u6587\u4ef6\u63cf\u8ff0\u7b26<\/td>\n<td style=\"text-align:center;width:332px\">fd :&#061; f.Fd()<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:119px\">syscall.Flock()<\/td>\n<td style=\"text-align:center;width:161px\">\u6587\u4ef6\u9501&#xff08;\u9700syscall&#xff09;<\/td>\n<td style=\"text-align:center;width:332px\">err :&#061; syscall.Flock(int(fd), syscall.LOCK_EX)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>2.\u5185\u5b58\u6620\u5c04<\/p>\n<p>import &#034;golang.org\/x\/exp\/mmap&#034;<\/p>\n<p>reader, _ :&#061; mmap.Open(&#034;file.dat&#034;)<br \/>\nbuf :&#061; make([]byte, 1024)<br \/>\nn, _ :&#061; reader.ReadAt(buf, 0)<\/p>\n<h2>\u4e03.\u4fbf\u6377\u51fd\u6570<\/h2>\n<p>1.\u5feb\u901f\u8bfb\u5199<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:601px\">\n<tbody>\n<tr>\n<td style=\"text-align:center;width:148px\">\u51fd\u6570<\/td>\n<td style=\"text-align:center;width:159px\">\u529f\u80fd<\/td>\n<td style=\"text-align:center;width:290px\">\u793a\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">os.ReadFile()<\/td>\n<td style=\"text-align:center;width:159px\">\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u00a0<\/td>\n<td style=\"text-align:center;width:290px\">data, err :&#061; os.ReadFile(&#034;file.txt&#034;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center;width:148px\">os.WriteFile()<\/td>\n<td style=\"text-align:center;width:159px\">\u5199\u5165\u6574\u4e2a\u6587\u4ef6<\/td>\n<td style=\"text-align:center;width:290px\">err :&#061; os.WriteFile(&#034;file.txt&#034;, data, 0644)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>2.\u6587\u4ef6\u5b58\u5728\u68c0\u67e5<\/p>\n<p>func FileExists(path string) bool {<br \/>\n    _, err :&#061; os.Stat(path)<br \/>\n    return !os.IsNotExist(err)<br \/>\n}<\/p>\n<h2>\u516b.\u9519\u8bef\u5904\u7406<\/h2>\n<p>\u5e38\u89c1\u9519\u8bef\u7c7b\u578b\u5224\u65ad&#xff1a;<\/p>\n<p>if errors.Is(err, os.ErrNotExist) {<br \/>\n    \/\/ \u6587\u4ef6\u4e0d\u5b58\u5728<br \/>\n} else if errors.Is(err, os.ErrExist) {<br \/>\n    \/\/ \u6587\u4ef6\u5df2\u5b58\u5728<br \/>\n} else if errors.Is(err, os.ErrPermission) {<br \/>\n    \/\/ \u6743\u9650\u4e0d\u8db3<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb177\u6b21\u30021.\u6587\u4ef6\u6253\u5f00\u4e0e\u521b\u5efa2.\u6587\u4ef6\u8bfb\u51993.\u6587\u4ef6\u5b9a\u4f4d4.\u6587\u4ef6\u5173\u95ed\u4e0e\u540c\u6b65\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[84],"topic":[],"class_list":["post-52534","post","type-post","status-publish","format-standard","hentry","category-server","tag-android"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>go\u8bed\u8a00\u4e2dos\u6587\u4ef6\u7c7b\uff0c\u6309\u7c7b\u522b\u6574\u7406\u6838\u5fc3\u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/52534.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"go\u8bed\u8a00\u4e2dos\u6587\u4ef6\u7c7b\uff0c\u6309\u7c7b\u522b\u6574\u7406\u6838\u5fc3\u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb177\u6b21\u30021.\u6587\u4ef6\u6253\u5f00\u4e0e\u521b\u5efa2.\u6587\u4ef6\u8bfb\u51993.\u6587\u4ef6\u5b9a\u4f4d4.\u6587\u4ef6\u5173\u95ed\u4e0e\u540c\u6b65\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/52534.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-11T07:39:05+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/52534.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/52534.html\",\"name\":\"go\u8bed\u8a00\u4e2dos\u6587\u4ef6\u7c7b\uff0c\u6309\u7c7b\u522b\u6574\u7406\u6838\u5fc3\u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-11T07:39:05+00:00\",\"dateModified\":\"2025-08-11T07:39:05+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/52534.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/52534.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/52534.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"go\u8bed\u8a00\u4e2dos\u6587\u4ef6\u7c7b\uff0c\u6309\u7c7b\u522b\u6574\u7406\u6838\u5fc3\u64cd\u4f5c\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"go\u8bed\u8a00\u4e2dos\u6587\u4ef6\u7c7b\uff0c\u6309\u7c7b\u522b\u6574\u7406\u6838\u5fc3\u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/52534.html","og_locale":"zh_CN","og_type":"article","og_title":"go\u8bed\u8a00\u4e2dos\u6587\u4ef6\u7c7b\uff0c\u6309\u7c7b\u522b\u6574\u7406\u6838\u5fc3\u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb177\u6b21\u30021.\u6587\u4ef6\u6253\u5f00\u4e0e\u521b\u5efa2.\u6587\u4ef6\u8bfb\u51993.\u6587\u4ef6\u5b9a\u4f4d4.\u6587\u4ef6\u5173\u95ed\u4e0e\u540c\u6b65\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/52534.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-11T07:39:05+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/52534.html","url":"https:\/\/www.wsisp.com\/helps\/52534.html","name":"go\u8bed\u8a00\u4e2dos\u6587\u4ef6\u7c7b\uff0c\u6309\u7c7b\u522b\u6574\u7406\u6838\u5fc3\u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-11T07:39:05+00:00","dateModified":"2025-08-11T07:39:05+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/52534.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/52534.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/52534.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"go\u8bed\u8a00\u4e2dos\u6587\u4ef6\u7c7b\uff0c\u6309\u7c7b\u522b\u6574\u7406\u6838\u5fc3\u64cd\u4f5c"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/52534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=52534"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/52534\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=52534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=52534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=52534"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=52534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}