{"id":15460,"date":"2024-11-18T18:10:45","date_gmt":"2024-11-18T10:10:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/?p=15460"},"modified":"2024-11-18T18:10:45","modified_gmt":"2024-11-18T10:10:45","slug":"go%e4%b8%ad%e7%9a%84os%e5%8c%85%e4%b9%8b%e7%9b%ae%e5%bd%95%e5%92%8c%e6%96%87%e4%bb%b6file%e7%9a%84%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/15460\/","title":{"rendered":"Go\u4e2d\u7684os\u5305\u4e4b\u76ee\u5f55\u548c\u6587\u4ef6(file)\u7684\u64cd\u4f5c"},"content":{"rendered":"<p>go\u4e2d\u5bf9\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u64cd\u4f5c\u4e3b\u8981\u96c6\u4e2d\u5728os\u5305\u4e2d\uff0c\u4e0b\u9762\u5bf9go\u4e2d\u7528\u5230\u7684\u5bf9\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u64cd\u4f5c\uff0c\u505a\u4e00\u4e2a\u603b\u7ed3\u7b14\u8bb0\u3002\u5728go\u4e2d\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u6d89\u53ca\u5230\u4e24\u79cd\u7c7b\u578b\uff0c\u4e00\u4e2a\u662f type File struct\uff0c\u53e6\u4e00\u4e2a\u662ftype Fileinfo interface\uff0c\u6765\u770b\u4e0b\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u5b9a\u4e49\uff1a<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>\u4e00\u3001 type File struct \u7684\u5b9a\u4e49<\/strong><\/h3>\n<pre>type File struct {\r\n   *file \/\/ os specific\r\n}\r\n\r\ntype file struct {\r\n   fd      int\r\n   name    string\r\n   dirinfo *dirInfo \/\/ nil unless directory being read\r\n}<\/pre>\n<h3><strong>\u4e8c\u3001type Fileinfo interface \u7684\u5b9a\u4e49<\/strong><\/h3>\n<pre>type FileInfo interface {\r\n   Name() string       \/\/ base name of the file\r\n   Size() int64        \/\/ length in bytes for regular files; system-dependent for others\r\n   Mode() FileMode     \/\/ file mode bits\r\n   ModTime() time.Time \/\/ modification time\r\n   IsDir() bool        \/\/ abbreviation for Mode().IsDir()\r\n   Sys() interface{}   \/\/ underlying data source (can return nil)\r\n}<\/pre>\n<h3>\u4e09\u3001\u6587\u4ef6\u6807\u5fd7<\/h3>\n<pre>const (\r\n\u00a0\u00a0\u00a0 O_RDONLY int = syscall.O_RDONLY \/\/ \u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\r\n\u00a0\u00a0\u00a0 O_WRONLY int = syscall.O_WRONLY \/\/ \u53ea\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\r\n\u00a0\u00a0\u00a0 O_RDWR\u00a0\u00a0 int = syscall.O_RDWR\u00a0\u00a0 \/\/ \u8bfb\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\r\n\u00a0\u00a0\u00a0 O_APPEND int = syscall.O_APPEND \/\/ \u5199\u64cd\u4f5c\u65f6\u5c06\u6570\u636e\u9644\u52a0\u5230\u6587\u4ef6\u5c3e\u90e8,\u9ed8\u8ba4\u662f\u8986\u76d6\r\n\u00a0\u00a0\u00a0 O_CREATE int = syscall.O_CREAT\u00a0 \/\/ \u5982\u679c\u4e0d\u5b58\u5728\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\r\n\u00a0\u00a0\u00a0 O_EXCL\u00a0\u00a0 int = syscall.O_EXCL\u00a0\u00a0 \/\/ \u548cO_CREATE\u914d\u5408\u4f7f\u7528\uff0c\u6587\u4ef6\u5fc5\u987b\u4e0d\u5b58\u5728\r\n\u00a0\u00a0\u00a0 O_SYNC\u00a0\u00a0 int = syscall.O_SYNC\u00a0\u00a0 \/\/ \u6253\u5f00\u6587\u4ef6\u7528\u4e8e\u540c\u6b65I\/O\r\n\u00a0\u00a0\u00a0 O_TRUNC\u00a0 int = syscall.O_TRUNC\u00a0 \/\/ \u5982\u679c\u53ef\u80fd\uff0c\u6253\u5f00\u65f6\u6e05\u7a7a\u6587\u4ef6\r\n)\r\n\r\n# \u5e76\u975e\u6587\u4ef6\u6743\u9650\u3002<\/pre>\n<p><strong>\u4e09\u3001\u5e38\u7528\u6784\u5efatype File\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u6e05\u695a\u4e86\u89e3File \u548c FileInfo\u7684\u5b9a\u4e49\u540e\uff0c\u63a5\u4e0b\u6765\u5bf9\u4ed6\u4eec\u7684\u7528\u6cd5\u5c31\u6bd4\u8f83\u5bb9\u6613\u7406\u89e3\u548c\u8bb0\u4f4f\u4e86\u3002<\/p>\n<p><strong>1\u3001func Create(name string) (file *File, err error)<\/strong><\/p>\n<p>\u51fd\u6570\u539f\u578b\uff1a<\/p>\n<pre>OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)<\/pre>\n<p>\u521b\u5efa\u6587\u4ef6\uff0c\u8be5\u65b9\u6cd5\u53ea\u80fd\u521b\u5efa\u6587\u4ef6\u3002\u4ece\u539f\u578b\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u521b\u5efa\u6743\u9650\u4e3a0666\u3001\u540d\u79f0\u4e3aname\u7684\u6587\u4ef6\uff0c\u6587\u4ef6\u6807\u5fd7\u4e3a\u53ef\u8bfb\u5199\u3001\u53ef\u521b\u5efa\u3001\u5185\u5bb9\u6e05\u7a7a\u3002<\/p>\n<p><strong>Tips 1\uff1a<\/strong>\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6587\u4ef6\u6253\u5f00\u7684\u6807\u8bc6\uff0c\u4f46\u5e76\u4e0d\u4fee\u6539\u6587\u4ef6\u6743\u9650\u3002<\/p>\n<p><strong>Tips 2\uff1a<\/strong>\u6587\u4ef6\u521b\u5efa\u6743\u9650\u4e0d\u4e00\u81f4\uff0c\u8bf7\u53c2\u8003\uff1aGo\u65b0\u5efa\u6587\u4ef6\u6743\u9650\u4e0e\u8bbe\u7f6e\u4e0d\u7b26<\/p>\n<pre>file , _ := os.Create(\".\/aaaa\")\r\nfile , _  = os.Create(\"\/data\/wanda\/go\/src\/test\/bbbb\")<\/pre>\n<p><strong>2\u3001func Open(name string) (file *File, err error)<\/strong><\/p>\n<p>\u51fd\u6570\u539f\u578b\uff1a<\/p>\n<pre>OpenFile(name, O_RDONLY, 0)<\/pre>\n<p>\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u3002<\/p>\n<p><strong>3\u3001func OpenFile(name string, flag int, perm FileMode) (file *File, err error)<\/strong><\/p>\n<p>OpenFile\u662f\u4e00\u4e2a\u66f4\u4e00\u822c\u6027\u7684\u6587\u4ef6\u6253\u5f00\u51fd\u6570\uff0c\u5927\u591a\u6570\u8c03\u7528\u8005\u90fd\u5e94\u7528Open\u6216Create\u4ee3\u66ff\u672c\u51fd\u6570\u3002\u5b83\u4f1a\u4f7f\u7528\u6307\u5b9a\u7684\u9009\u9879\uff08\u5982O_RDONLY\u7b49\uff09\u3001\u6307\u5b9a\u7684\u6a21\u5f0f\uff08\u59820666\u7b49\uff09\u6253\u5f00\u6307\u5b9a\u540d\u79f0\u7684\u6587\u4ef6\u3002\u5982\u679c\u64cd\u4f5c\u6210\u529f\uff0c\u8fd4\u56de\u7684\u6587\u4ef6\u5bf9\u8c61\u53ef\u7528\u4e8eI\/O\u3002\u5982\u679c\u51fa\u9519\uff0c\u9519\u8bef\u5e95\u5c42\u7c7b\u578b\u662f*PathError\u3002<\/p>\n<p><strong>4\u3001func NewFile(fd uintptr, name string) *File<\/strong><\/p>\n<p>NewFile\u4f7f\u7528\u7ed9\u51fa\u7684Unix\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u540d\u79f0\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u3002<\/p>\n<p><strong>5\u3001func Pipe() (r *File, w *File, err error)<\/strong><\/p>\n<p>Pipe\u8fd4\u56de\u4e00\u5bf9\u5173\u8054\u7684\u6587\u4ef6\u5bf9\u8c61\u3002\u4ecer\u7684\u8bfb\u53d6\u5c06\u8fd4\u56de\u5199\u5165w\u7684\u6570\u636e\u3002\u672c\u51fd\u6570\u4f1a\u8fd4\u56de\u4e24\u4e2a\u6587\u4ef6\u5bf9\u8c61\u548c\u53ef\u80fd\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>go\u4e2d\u5bf9\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u64cd\u4f5c\u4e3b\u8981\u96c6\u4e2d\u5728os\u5305\u4e2d\uff0c\u4e0b\u9762\u5bf9go\u4e2d\u7528\u5230\u7684\u5bf9\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u64cd\u4f5c\uff0c\u505a\u4e00\u4e2a\u603b\u7ed3\u7b14\u8bb0\u3002\u5728go\u4e2d\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u6d89\u53ca\u5230\u4e24\u79cd\u7c7b\u578b\uff0c\u4e00\u4e2a\u662f type File struct\uff0c\u53e6\u4e00\u4e2a\u662ftype Fileinfo interface\uff0c\u6765\u770b\u4e0b\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u5b9a\u4e49\uff1a &nbsp; \u4e00\u3001 type File struct \u7684\u5b9a\u4e49 type File struct { *file \/\/ os specific } type file struct { fd int name string dirinfo *dirInfo \/\/ nil unless directory being read } \u4e8c\u3001type Fileinfo interface \u7684\u5b9a\u4e49 type FileInfo interface { Name() string \/\/ base name of the file Size() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-15460","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=15460"}],"version-history":[{"count":1,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15460\/revisions"}],"predecessor-version":[{"id":15461,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15460\/revisions\/15461"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=15460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=15460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=15460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}