SoFunction
Updated on 2025-03-04

How to operate Go language file

This article describes the method of operating Go language file. Share it for your reference. The details are as follows:

Close the file:

Copy the codeThe code is as follows:
func (file *File) Close() {
    if file == nil {
        return
    }
    e := ()
    = -1 // so it can't be closed again
    if e != 0 {
        return (e)
    }
    return nil
}

File reading:

Copy the codeThe code is as follows:
func (file *File) Read(b []byte) (ret int, err ) {
    if file == nil {
        return -1,
    }
    r, e := (, b)
    if e != 0 {
        err = (e)
    }
    return int(r), err
}

Write a file:

Copy the codeThe code is as follows:
func (file *File) Write(b []byte) (ret int, err ) {
    if file == nil {
        return -1,
    }
    r, e := (, b)
    if e != 0 {
        err = (e)
    }
    return int(r), err
}

Get the file name:

Copy the codeThe code is as follows:
func (file *File) String() string {
    return
}

I hope this article will be helpful to everyone's Go language programming.