GO語言"io/ioutil"包中"ReadFile"函數的用法及代碼示例。
用法:
func ReadFile(filename string)([]byte, error)
ReadFile 讀取以文件名命名的文件並返回內容。成功的調用返回 err == nil,而不是 err == EOF。因為ReadFile 讀取整個文件,它不會將Read 中的EOF 視為要報告的錯誤。
從 Go 1.16 開始,此函數僅調用 os ReadFile
例子:
package main
import (
"fmt"
"io/ioutil"
"log"
)
func main() {
content, err := ioutil.ReadFile("testdata/hello")
if err != nil {
log.Fatal(err)
}
fmt.Printf("File contents: %s", content)
}
輸出:
File contents: Hello, Gophers!
相關用法
- GO ReadFull用法及代碼示例
- GO ReadMessage用法及代碼示例
- GO Read用法及代碼示例
- GO Reader.Multistream用法及代碼示例
- GO ReadAtLeast用法及代碼示例
- GO Reader.Len用法及代碼示例
- GO Reader用法及代碼示例
- GO ReadDir用法及代碼示例
- GO Reader.ReadAll用法及代碼示例
- GO ReadAll用法及代碼示例
- GO Regexp.FindString用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
- GO ResponseRecorder用法及代碼示例
- GO ReverseBytes64用法及代碼示例
- GO ReverseBytes16用法及代碼示例
- GO Regexp.ReplaceAllLiteralString用法及代碼示例
- GO Regexp.FindStringSubmatch用法及代碼示例
- GO Regexp.FindAllString用法及代碼示例
- GO Regexp.ExpandString用法及代碼示例
- GO ResponseWriter用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 ReadFile。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。