File

File 相關 API 會使用 os 模組創建檔案或開啟檔案。

創建檔案並寫入

package main

import (
	"fmt"
	"os"
)

func main() {
	f, err := os.Create("test.txt")
	defer f.Close()
	if err != nil {
		fmt.Println(err)
	}
	l, err := f.WriteString("apple")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("How many bytes write to file: ", l)
}

Append 檔案

加上新字段在舊檔案,os.OpenFile 一定要給三個參數,然後第二個參數如果要append必須是os.O_APPEND|os.O_WRONLY

寫入 Bytes

在 txt 檔案內會顯示對應的 ASCII 字元

Last updated

Was this helpful?