Using the OS to complete file operations (common)

Source: Internet
Author: User
Need to use Golang to complete a program to write to the file, the first time to use these functions under the OS, give yourself a document. Look at the source of the process, found using Golang file creation functions, need to understand the knowledge of the file permission bit (Ps:java has never been a method to solve, never carefully understand), here to put a [explanation of the Authority bit] (https://blog.csdn.net/ devyfong/article/details/8066369) 's blog. I use the IDE is IntelliJ Goland, see the source is very convenient, recommend and I like beginners use # # func getwd (dir string, err Error) {...} Get the program run path, equivalent to running ' pwd ' command '//code package Mainimport ("FMT" "OS") Func main () {FMT] under Linux. Println (OS. GETWD ())}//command line execution bogon:postback zhaoyf$ go run application.go/users/zhaoyf/code/go_path/src/adr-postback-consumer/ Postback <nil>bogon:postback zhaoyf$ CD ~bogon:~ zhaoyf$ go run code/go_path/src/adr-postback-consumer/postback/ Application.go/users/zhaoyf <nil> ' # # # func isnotexist (err error) bool {...} Determine if err is not present in the hint file or directory "//-----Source//Error.go-isnotexist () Func isnotexist (err error) bool {return isnotexist (err)}// Error_unix.go-Isnotexist ()//My environment is Mac, possibly other system file names are different func isnotexist (err error) bool {err = Underlyingerror (ERR)//Contemporary Code See this step to find out that this method is used to compare the parameter err is not a specified error return err = = Syscall. ENOENT | | Err = = Errnotexist}//Error.go-Underlyingerror ()//returns the underlying error of the known OS error type Func underlyingerror (err Error) error {switch err: = Err. (type) {Case *patherror:return err. Errcase *linkerror:return Err. Errcase *syscallerror:return Err. Err}return err} ' # # # func Mkdir (name string, perm FileMode) error {...} Creates a folder based on the given path [name] and specifies the folder's permissions through Perm "///self-Encapsulated Create folder function func mkdir (path string) {err: = OS. Mkdir (Path, OS. MODEDIR)//When error is returned and error is not a file that already exists errors, print the wrong message if err! = Nil && os. Isnotexist (Err) {FMT. Printf ("[init] Create log dir[%s] falied:%s\n", Path, Err)}} ' # # func OpenFile (name string, flag int, perm FileMode) (* File, error) {...} Return a method that can perform I/O operations, read and write by the *file of the operation return value, note that the release of the stream resources "//the specific constant code length is too long, please look for your own//flag value as shown below see File.go-65 Row constant definition//Perm value See Types.go, 42 Line Constants Definition ' # # # func-Create (name string) (*file, error) {...} Do not explain, directly look at the source bar "" Func Create (name string) (*file, error) {//If the o_* constant is not understood, you can see the OpenFile () function above the flag value return OpenFile (name, o_rdwr| o_create| O_trunc, 0666)} ' ' # # # func (f *file) WRite (b []byte) (n int, err error) {...} Writes the file, returns the number of bytes written successfully "//Steal a lazy, example directly affixed writestring () function of the source//writestring is like Write, but writes the contents of string s rather than//a slice of Bytes.func (f *file) WriteString (s string) (n int, err error) {return F.write ([]byte (s))}//the written text of your own encapsulated letter Number of Func write (log string, FilePath string) {file, err: = OS. OpenFile (FilePath, OS. O_wronly|os. O_create|os. O_append, 0666) if err! = Nil && os. Isnotexist (Err) {FMT. Printf ("[Write] Create log file[%s] falied:%s\n", FilePath, Err)}defer file. Close () file. WriteString (log + "\ n")} ' # # # OS. PathSeparator Add a small egg, file path delimiter, win with ' \ \ ' Unix with '/', in order to avoid writing in the code of the bug caused by death, you can use this constant instead of 83 clicks  

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.