LoginSignup
1
2

More than 5 years have passed since last update.

golang ioテンプレート

Last updated at Posted at 2017-01-19

ファイルを作成して、書き込み

package main

import (
    "fmt"
    "os"
)

func main() {

  file, err := os.Create("foo.txt")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer file.Close()

  bf := bufio.NewWriter(file)

  // 文字列を書き込むなら`WriteString`の方がおそらく高速。
  // なぜなら、[]byteにキャストをするとメモリコピーが発生するから
  // `WriteString`は内部でポインタで参照している...と思う
  bf.Write([]byte("Hello World\n"))
  bf.WriteString("Hello World\n")

  fmt.Println(file.Fd())
}

※ []byteキャストのことはここに詳しく書かれています。

参考サイト

1
2
1

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
2