Go文件I/O

在运行中, os.file对象用于文件操作。 os.File对象也称为文件句柄。

os包中的open函数用于在Go中打开文件。 io / ioutil包中的ReadFile()用于读取文件。此方法返回[] byte个读取字节的数组。 file.WriteString方法可用于写入文件。

我们在打开文件后立即使用defer file.close()来确保函数完成后立即关闭文件。如果文件不存在或程序没有足够的权限打开文件, 则

inputFile, inputError = os.Open(“ input.dat”)导致错误。

Go文件I / O示例

package main
import (
   "os"
   "log"
   "io/ioutil"
   "fmt"
)
func main() {
   file, err := os.Create("file.txt")
   if err != nil {
      log.Fatal(err)
   }
   file.WriteString("Hi... there")
   file.Close()
   stream, err:= ioutil.ReadFile("file.txt")
   if err != nil {
      log.Fatal(err)
   }
   readString := string(stream)
   fmt.Println(readString)
}

输出:

Hi... there
微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_50013.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?