SoFunction
Updated on 2025-03-04

Summary of file operation codes for Go language

# Create and open files

// Create a new file through the following two methods:

func Create(name string) (file *File, err Error)

Create a new file based on the provided file name and return a file object. The default permission is 0666. The returned file object is readable and writeable.

func NewFile(fd uintptr, name string) *File

Create the corresponding file according to the file descriptor and return a file object

// Open the file through the following two methods:

func Open(name string) (file *File, err Error)

This method opens a file named name, but it is read-only, and the internal implementation actually calls OpenFile.

func OpenFile(name string, flag int, perm uint32) (file *File, err Error)

Open a file with name, flag is the way to open it, read only, read and write, etc., perm is the permission

// Write a file

func (file *File) Write(b []byte) (n int, err Error)

Write information of type byte to the file

func (file *File) WriteAt(b []byte, off int64) (n int, err Error)

Start writing information of type byte at the specified location

func (file *File) WriteString(s string) (ret int, err Error)

Write string information to file

// Read the file

func (file *File) Read(b []byte) (n int, err Error)

Read data into b

func (file *File) ReadAt(b []byte, off int64) (n int, err Error)

Start reading data into b from off

// Delete the file

func Remove(name string) Error

Call this function to delete a file with the file name

//// Close the file

func (file *File)Close()

Write a file

// code_034_os_write_to_file project 
package main

import (
  "fmt"
  "os"
)

func main() {

  // Create a new file  fout, err := ("./")
  if err != nil {
    (err)
    return
  }
  defer ()

  for i := 0; i < 5; i++ {
    //Note: In Windows environment, the ending \r\n can be opened, and under linux\n can be used    outstr := ("%s:%d\r\n", "Hello Go", i) //Sprintf console output and return value string    // Write to the file    (outstr)             //string information    ([]byte("abcd\r\n"))          //byte type  }
}

Read the file

// code_035_os_read_from_file project 
package main

import (
  "fmt"
  "os"
)

func main() {
  fin, err := ("./open_and_read1.txt")
  if err != nil {
    (err) //If the file does not exist: The system cannot find the file specified.  }
  defer ()

  buf := make([]byte, 1024) //Create a storage slice  for {
    n, _ := (buf) //Read the file    if n == 0 {
      break
    }
    (string(buf))
  }

}

Copy the file ----》 (Note: local_copy_file.txt that has been created and written) The terminal switches to the current directory and executes go run local_copy_file.txt dst_file.txt

// code_036_os_copy_file project 
package main

import (
  "fmt"
  "io"
  "os"
)

func main() {
  // Use the command line to improve copy reusability  args := 
  if args == nil || len(args) != 3 {
    ("useage : go  src File dstFile")
    return
  }

  srcPath := args[1]
  dstPath := args[2]
  ("srcPath = %s, dstPath = %s\r\n", srcPath, dstPath)

  if srcPath == dstPath {
    ("The source file and the target file cannot be renamed")
  }

  //Execute copy  srcFile, err1 := (srcPath)
  if err1 != nil {
    (err1)
    return
  }
  dstFile, err2 := (dstPath)
  if err2 != nil {
    (err2)
    return
  }
  read_buf := make([]byte, 1024)
  for {
    //Read the file    n, err := (read_buf) //The length of bytes is read per file    if err != nil && err !=  {
      (err)
      break
    }
    if n == 0 {
      ("File processing is complete")
      break
    }
    //Write to the destination file
    write_buf := read_buf[:n]
    (write_buf)
  }
  // Close the file  ()
  ()

}