goland讀取文件寫入文件


package main

import (
"fmt"
"io/ioutil"
"strings"
)
/**
* 讀取文件
* string name 文件名稱
*/
func ReadFile(name string) {
if contents,err := ioutil.ReadFile(name);err == nil {
result := strings.Replace(string(contents),"\n","",1)
fmt.Println("ReadFile:",result)
}
}
/**
* 寫入文件
* string name 文件名稱
* byte content 文件內容
*/
func WriteFile(name string, content []byte){
if ioutil.WriteFile(name, content, 0644) == nil{
fmt.Println("WriteFile")
}else{
fmt.Println("NOT WriteFile")
}
}

func main() {
name := "content.txt"
content := "content"
WriteFile(name, []byte(content))
ReadFile(name)
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM