舉例詳解Go語言中os庫的常用函數用法_Golang

來源:互聯網
上載者:User

(f *File).Name()這個函數是返迴文件的名稱,函數原型func (f *File) Name() string要檔案的指標操作,返回字串,感覺比較雞助的方法底層實現

複製代碼 代碼如下:

 func (f *File) Name() string { return f.name }
 import (
 "fmt"
 "os"
)

func main() {
 f, _ := os.Open("1.go")
 fmt.Println(f.Name()) //輸出1.go
}


(f *File).Read()這個是函數的指標來操作的,屬於*FIlE的method,函數原型func (f *File) Read(b []byte) (n int, err error)輸入讀取的位元組數,返回位元組的長度和error資訊
複製代碼 代碼如下:

import (
 "fmt"
 "os"
)

func main() {
 b := make([]byte, 100) //設定讀取的位元組數
 f, _ := os.Open("11.go")
 n, _ := f.Read(b)
 fmt.Println(n) 
 fmt.Println(string(b[:n])) //輸出內容 為什麼是n而不直接輸入100呢?底層這樣實現的
 /*
  n, e := f.read(b)
     if n < 0 {
       n = 0
     }
  if n == 0 && len(b) > 0 && e == nil {
     return 0, io.EOF
    }
   */
   //所以位元組不足100就讀取n
}


(f *File).ReadAt()這個函數的原型是func (f *File) ReadAt(b []byte, off int64) (n int, err error)加入了下標,可以自訂讀取多少
複製代碼 代碼如下:

import (
 "fmt"
 "os"
)

func main() {
 f, _ := os.Open("11.go")
 b := make([]byte, 20)
 n, _ := f.ReadAt(b, 15)
 fmt.Println(n)
 fmt.Println(string(b[:n]))
}


(f *File).Readdir()函數原型func (f *File) Readdir(n int) (fi []FileInfo, err error),我們要開啟一個檔案夾,然後設定讀取檔案夾檔案的個數,返回的是檔案的fileinfo資訊
複製代碼 代碼如下:

import (
 "fmt"
 "os"
)

func main() {
 f, err := os.Open("src") //開啟一個目錄
 if err != nil {
  fmt.Println(err)
 }
 defer f.Close()
 ff, _ := f.Readdir(10)    //設定讀取的數量 <=0是讀取所有的檔案 返回的[]fileinfo
 for i, fi := range ff {
  fmt.Printf("filename %d: %+v\n", i, fi.Name())  //我們輸出檔案的名稱
 }
}


(f *File).Readdirnames這個函數的作用是讀取目錄內的檔案名稱,其實上一個函數我們已經實現了這個函數的功能,函數的原型func (f *File) Readdirnames(n int) (names []string, err error),跟上邊一下只不過返回的是檔案名稱 []string的slice
複製代碼 代碼如下:

import (
 "fmt"
 "os"
)

func main() {
 f, _ := os.Open("bin")
 names, err := f.Readdirnames(0)
 if err != nil {
  fmt.Println(err)
 }
 for i, name := range names {
  fmt.Printf("filename %d: %s\n", i, name)
 }
}


(f *File).Seek()這個函數大家一看就懂了,就是位移指標的地址,函數的原型是func (f *File) Seek(offset int64, whence int) (ret int64, err error) 其中offset是檔案指標的位置 whence為0時代表相對檔案開始的位置,1代表相對當前位置,2代表相對檔案結尾的位置 ret返回的是現在指標的位置
複製代碼 代碼如下:

import (
 "fmt"
 "os"
)

func main() {
 b := make([]byte, 10)
 f, _ := os.Open("1.go")
 defer f.Close()
 f.Seek(1, 0)    //相當於開始位置位移1
 n, _ := f.Read(b)
 fmt.Println(string(b[:n]))  //原字元package 輸出ackage
}


(f *File) Write像檔案中寫入內容,函數原型func (f *File) Write(b []byte) (n int, err error)返回的是n寫入的位元組數
複製代碼 代碼如下:

import (
 "fmt"
 "os"
)

func main() {
 f, _ := os.OpenFile("1.go", os.O_RDWR|os.O_APPEND, 0755) //以追加和讀寫的方式去開啟檔案
 n, _ := f.Write([]byte("helloword"))                     //我們寫入hellword
 fmt.Println(n)                                           //列印寫入的位元組數
 b := make([]byte, 20)
 f.Seek(0, 0)            //指標返回到0
 data, _ := f.Read(b)
 fmt.Println(string(b[:data]))        //輸出了packagehelloword
}


(f *File) WriteAt()在位移位置多少的地方寫入,函數原型是func (f *File) WriteAt(b []byte, off int64) (n int, err error)傳回值是一樣的
複製代碼 代碼如下:

import (
 "fmt"
 "os"
)

func main() {
 f, _ := os.OpenFile("1.go", os.O_RDWR, os.ModePerm)
 f.WriteAt([]byte("widuu"), 10) //在位移10的地方寫入
 b := make([]byte, 20)
 d, _ := f.ReadAt(b, 10)    //位移10的地方開始讀取
 fmt.Println(string(b[:d])) //widuudhellowordhello
}


(f *File).WriteString()這個很簡單了,寫入字串函數原型func (f *File) WriteString(s string) (ret int, err error)傳回值一樣的了
複製代碼 代碼如下:

import (
 "fmt"
 "os"
)

func main() {
 f, _ := os.OpenFile("2.go", os.O_RDWR, os.ModePerm)
 n, _ := f.WriteString("hello word widuu") //寫入字串
 fmt.Println(n)
 b := make([]byte, n)
 f.Seek(0, 0)    //一定要把位移地址歸0否則就一直在寫入的結尾處
 c, _ := f.Read(b)
 fmt.Println(string(b[:c])) //返回hello word widuu
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.