Go读写文件


package main

import (
	"fmt"
	"io/ioutil"
)

func ReadFile(file string) string {
	data, err := ioutil.ReadFile(file)
	if err != nil {
		return ""
	}
	return string(data)
}

func WriteFile(file string, content string) int {
	buffer := []byte(content)
	err := ioutil.WriteFile(file, buffer, 0666)
	if err != nil {
		return 0
	}
	return len(content)
}

func main() {
	write := WriteFile("1.txt", "abc")
	data := ReadFile("1.txt")
	fmt.Println(write)
	fmt.Println(data)
}