<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                文件創建 os包中實現了平臺無關的接口,設計向Unix風格,但是錯誤處理是go風格,當os包使用時,如果失敗之后返回錯誤類型而不是錯誤數量. 創建目錄: ~~~ func Getwd() (dir string, err error) //獲取當前目錄,類似linux中的pwd func Mkdir(name string, perm FileMode) error  //創建一個新目錄,該目錄具有FileMode權限,當創建一個已經存在的目錄時會報錯 func IsExist(err error) bool       /* 返回一個布爾值,它指明err錯誤是否報告了一個文件或者目錄已經存在。 它被ErrExist和其它系統調用滿足。 */ func IsNotExist(err error) bool      /* 返回一個布爾值,它指明err錯誤是否報告了一個文件或者目錄不存在。 它被ErrNotExist 和其它系統調用滿足。 */ func MkdirAll(path string, perm FileMode) error  /* 創建一個新目錄,該目錄是利用路徑(包括絕對路徑和相對路徑)進行創建的, 如果需要創建對應的父目錄,也一起進行創建,如果已經有了該目錄, 則不進行新的創建,當創建一個已經存在的目錄時,不會報錯. */ func Rename(oldpath, newpath string) error   //重命名文件,如果oldpath不存在,則報錯no such file or directory ~~~ 代碼實現: ~~~ package main import ( "fmt" "os" ) func main() { path, err := os.Getwd() if err != nil { fmt.Printf("get path err : %v\n", err) } fmt.Printf("當前目錄 : %v\n", path) err = os.Mkdir("./golang", 0777) if err != nil { fmt.Printf("mkdir golang err : %v\n", err) if os.IsExist(err) { fmt.Println("文件已存在!") } if os.IsNotExist(err) { fmt.Println("文件不存在!") } } err = os.MkdirAll("./golang/go", 0777) if err != nil { fmt.Println("mkdirall err : %v\n", err) } err = os.Rename("./golang/go", "./golang/gogo") if err != nil { fmt.Printf("rename err : %v\n", err) } } ~~~ 創建文件: ~~~ func Create(name string) (file *File, err error) /* Create采用模式0666(任何人都可讀寫,不可執行) 創建一個名為name的文件,如果文件已存在會截斷它(為空文件)。 如果成功,返回的文件對象可用于I/O;對應的文件描述符具有O_RDWR模式。 如果出錯,錯誤底層類型是*PathError。 其相當于OpenFile的快捷操作,等同于OpenFile(name string, O_CREATE,0666) */ func NewFile(fd uintptr, name string) *File // NewFile使用給出的Unix文件描述符和名稱創建一個文件。 func (f *File) Close() error // Close關閉文件f,使文件不能用于讀寫。它返回可能出現的錯誤。 func (f *File) Name() string // Name方法返回(提供給Open/Create等方法的)文件名稱。 func (f *File) Stat() (fi FileInfo, err error) // Stat返回描述文件f的FileInfo類型值。如果出錯,錯誤底層類型是*PathError。 func SameFile(fi1, fi2 FileInfo) bool /* SameFile返回fi1和fi2是否在描述同一個文件。 例如,在Unix這表示二者底層結構的設備和索引節點是相同的;在其他系統中可能是根據路徑名確定的。 SameFile應只使用本包Stat函數返回的FileInfo類型值為參數,其他情況下,它會返回假。 */ type FileInfo interface { Name() string // 文件的名字(不含擴展名) Size() int64 // 普通文件返回值表示其大小;其他文件的返回值含義各系統不同 Mode() FileMode // 文件的模式位 ModTime() time.Time // 文件的修改時間 IsDir() bool // 等價于Mode().IsDir() Sys() interface{} // 底層數據來源(可以返回nil) } func (m FileMode) IsRegular() bool // IsRegular報告m是否是一個普通文件。 func (m FileMode) IsDir() bool // IsDir報告m是否是一個目錄。 ~~~ 代碼實現: ~~~ package main import ( "fmt" "os" "syscall" ) func main() { file1, err := os.Create("./file1.txt") if err != nil { fmt.Printf("create file1 err : %v\n", err) } if file1 != nil { defer func(file *os.File) { file.Close() }(file1) fmt.Println("create file1 success ") } file2 := os.NewFile(uintptr(syscall.Stdin), "./file2.txt") //標準輸入 // file2 := os.NewFile(uintptr(syscall.Stdout), "./file2.txt")//標準輸出 // file2 := os.NewFile(uintptr(syscall.Stderr), "./file2.txt") if file2 != nil { defer func(file *os.File) { file.Close() }(file2) fmt.Println("newfile file2 success ") } fileName := file1.Name() fmt.Printf("file1 name is %v\n", fileName) fileInfo1, err := file1.Stat() if err != nil { fmt.Printf("get file1 info err : %v\n", err) } fmt.Println(fileInfo1) fileInfo2, err := file2.Stat() if err != nil { fmt.Printf("get file2 info err : %v\n", err) } fmt.Println(fileInfo2) b := os.SameFile(fileInfo1, fileInfo1) if b { fmt.Println("fileInfo1 與 fileInfo1 是同一個文件") } else { fmt.Println("fileInfo1 與 fileInfo1 不是同一個文件") } fileMode1 := fileInfo1.Mode() b = fileMode1.IsRegular() if b { fmt.Println("file1 是普通文件") } else { fmt.Println("file1 不是普通文件") } b = fileMode1.IsDir() if b { fmt.Println("file1 是普通目錄") } else { fmt.Println("file1 不是普通目錄") } } ~~~ 輸出結果: ~~~ create file1 success newfile file2 success file1 name is ./file1.txt &{file1.txt 0 420 {913174048 63658953067 0x113f680} {16777220 33188 1 8595697404 501 20 0 [0 0 0 0] {1523353864 679519948} {1523356267 913174048} {1523356267 913174048} {1523353864 679519948} 0 0 4194304 0 0 0 [0 0]}} &{file2.txt 0 69206416 {913348000 63658953067 0x113f680} {808439728 8592 1 2437 501 4 268435456 [0 0 0 0] {1523356267 358947000} {1523356267 913348000} {1523356267 913348000} {0 0} 0 0 131072 0 0 0 [0 0]}} fileInfo1 與 fileInfo1 是同一個文件 file1 是普通文件 file1 不是普通目錄 ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看