Basic operation of Golang file (file open, file write, file read, file delete)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
The code is as follows Copy Code

Package Main

Import (
"Bufio"//Cache IO
"FMT"
"Io/ioutil"//io Toolkit
"IO"
"OS"
)

Func Check (e error) {
If E! = Nil {
Panic (e)
}
}

/**
* Determine if a file exists return true does not exist return false
*/
Func checkfileisexist (filename string) (bool) {
var exist = true;
If _, Err: = OS. Stat (filename); Os. Isnotexist (Err) {
exist = false;
}
return exist;
}

/**
from:http://www.isharey.com/?p=143
*/

Func Main () {
 var wiretestring = "Test n"
 var filename = "./output1.txt";
 var f    *os. File
 var err1   error;
 /***************************** first way: Using IO. WriteString Write file ***********************************************/
 if checkfileisexist (filename) { // If the file exists
  f, err1 = os. OpenFile (filename, os. O_append, 0666)  //Open file
  fmt. Println ("file exists");
 }else {
  f, err1 = os. Create (FileName)  //Creation file
  fmt. PRINTLN ("File does not exist");
 }
 check (ERR1)
 n, err1: = Io. WriteString (f, wiretestring)//write to file (string)
 check (ERR1)
 fmt. Printf ("write%d bytes n", n);

 /*****************************  Second way: Use Ioutil. WriteFile Write file ***********************************************/
 var d1 = []byte (wiretestring);
 ERR2: = Ioutil. WriteFile ("./output2.txt", D1, 0666)  //write file (byte array)
 check (ERR2)

/***************************** Third Way: Write files using file (write,writestring) ********************************************* **/
F, ERR3: = OS. Create ("./output3.txt")//File creation
Check (ERR3)
Defer F.close ()
N2, Err3: = F.write (D1)//write to file (byte array)
Check (ERR3)
Fmt. Printf ("write%d bytes n", N2)
N3, Err3: = f.writestring ("WRITESN")//write to file (byte array)
Fmt. Printf ("write%d bytes n", N3)
F.sync ()
/***************************** Fourth Way: Use Bufio. Newwriter writing to File ***********************************************/
W: = Bufio. Newwriter (f)//Create a new Writer object
N4, Err3: = w.writestring ("Bufferedn")
Fmt. Printf ("write%d bytes n", N4)
W.flush ()
F.close ()
}

Transfer from http://www.android100.org/html/201407/28/47230.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.