1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

go ファイルの読み取り

Posted at

Goでファイルの読み取り方法を学んだのでまとめておきます。

main.go
package main

import(
    "fmt"
    "os"
)

func main() {

//file変数にファイルのデータを代入
//ここではエラーは空白に
file, _:= os.Open("ファイル名")

//ファイルのクローズを最後に行うためのdefer
defer file.Close()

//バイト配列を作成
data := make([]byte, 100)

//バイト配列にファイルのデータを読み込む
file.Read(data)

//バイト配列に格納されたデータを出力
//バイト型になっているので、string()で文字列に型変換を行う
fmt.Println(string(data))
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?