Go language Programming-go language implementation file copy __go language programming

Source: Internet
Author: User
Introduction to the File Operations API New File

Func Create (name string) (file *file, err Error)
Creates a new file based on the file name provided, returns a file object with the default permission of 0666, and the returned file object is writable.

Func NewFile (fd uintptr, name string) *file
Creates the appropriate file based on the file descriptor, returns a file object to open the file

Func Open (name string) (file *file, err Error)
The method opens a file named name, but is read-only, and the internal implementation actually invokes OpenFile.

Func OpenFile (name string, flag int, perm uint32) (file *file, err Error)
Open the file named Name, flag is open way, read only, read and write, perm is permission to write files

Func (file *file) Write (b []byte) (n int, err Error)
Writes byte-type information to a file

Func (file *file) writeat (b []byte, off Int64) (n int, err Error)
Writes byte-type information at a specified location

Func (file *file) writestring (S string) (ret int, err Error)
Write string information to file read file

Func (file *file) Read (b []byte) (n int, err Error)
reading data to B

Func (file *file) readat (b []byte, off Int64) (n int, err Error)
Read data from off to delete files in B

Func Remove (name string) Error
Call this function to delete file code implementation with filename name

Package main

Import (
    "FMT"
    "io" "
    os"
)

func main () {
    list: = OS. Args

    If Len (list)!= 3 {
        FMT. Println ("Error: Missing parameters")
        return
    }
    srcname: = list[1]
    dstname: = list[2]
    srcfile, err1: = OS. Open (SrcName)
    dstfile, ERR2: = OS. Create (dstname)
    if err1!= nil | | err2!= NIL {fmt
        . Println ("err")
        return
    }
    defer srcfile.close ()
    defer dstfile.close ()
    buf: = Make ([]byte, 4* 1024)//4k buffer
    //core processing for
    {
        N, err: = Srcfile.read (BUF)
        If err!= nil {
            if err = = Io. EOF {//file read complete
                break
            }
            FMT. Println ("ERR:", err)
        }
        dstfile.write (Buf[:n])//Read how many to write}
}
Run Detection


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.