1.创建文件
filePath:="./show.txt" _,err:=os.Create(filePath) if err!= nil { fmt.Println("create file is error") } _,err:=os.OpenFile(filePath,os.O_APPEND|os.O_CREATE,0666) if err != nil { fmt.Println("create file is error") }
2.写文件
//第一种办法 filePath:="./show.txt" file,err:=os.Create(filePath) if err != nil{ fmt.Println("create file is error") } content:=[]byte("this is file content") n,err:=file.Write(content) if err != nil { fmt.Printf("write file is error") } fmt.Print(n) defer file.Close() //第二种办法 filePath:="./show.txt" file,err:=os.OpenFile(filePath,os.O_CREATE|os.O_APPEND,0666) if(err != nil ){ fmt.Println("create file is error") } content:="this is file test content" newWriter:=bufio.NewWriter(file) newWriter.WriteString(content) newWriter.Flush() defer file.Close() //第三种办法 filePath:="info.txt" content:=[]byte("this is infio") err:=ioutil.WriteFile(filePath,content,0666) if err != nil { fmt.Println(err) }