Go can manipulate files in many ways, but this is just a tidy up for a record. All the code can be used directly, the code is very simple, I won’t explain more.

Read the file

Read files through os.open

The following code:

func TestReadFile(t *testing.T) {
    fileName := "/tmp/mockdata.txt"
    file, err := os.Open(fileName)
    defer closeResource(file)
    iferr ! =nil {
        log.Fatalf("failed to open file %s", fileName)
    }

    var tempSlice = make([]byte.128)
    var strSlice []byte
    for {
        n, err := file.Read(tempSlice)
        if err == io.EOF {
            log.Printf("%s is read compeletely.", fileName)
            break
        }
        iferr ! =nil {
            log.Fatalf("failed to read file.")
        }
        log.Printf("read %d bytes\n", n)
        strSlice = append(strSlice, tempSlice[:n]...)
    }
    log.Println(string(strSlice))
}

func closeResource(closeable io.Closer) {
    ifcloseable ! =nil {
        iferr := closeable.Close(); err ! =nil {
            log.Errorln("failed to close resource.", err)
        }
    }
}
Copy the code

Read files through Bufio

The following code:

func TestReadFile(t *testing.T) {
    fileName := "/tmp/mockdata.txt"
    file, err := os.Open(fileName)
    defer closeResource(file)
    iferr ! =nil {
        log.Fatalf("failed to open file %s", fileName)
    }

    var tempSlice = make([]byte.128)
    var strSlice []byte
    for {
        n, err := file.Read(tempSlice)
        if err == io.EOF {
            log.Printf("%s is read compeletely.", fileName)
            break
        }
        iferr ! =nil {
            log.Fatalf("failed to read file.")
        }
        log.Printf("read %d bytes\n", n)
        strSlice = append(strSlice, tempSlice[:n]...)
    }
    log.Println(string(strSlice))
}

func closeResource(closeable io.Closer) {
    ifcloseable ! =nil {
        iferr := closeable.Close(); err ! =nil {
            log.Errorln("failed to close resource.", err)
        }
    }
}
Copy the code

Read files through ioutil

The following code:

func TestReadFileByIOUtil(t *testing.T) {
    fileName := "/tmp/mockdata.txt"
    bytes, err := ioutil.ReadFile(fileName)
    iferr ! =nil {
        log.Fatalf("failed to open file %s", fileName)
    }
    t.Log(string(bytes))
}
Copy the code

Write files

Write the file to file.write

The following code:

func TestWriteFile(t *testing.T) {
    fileName := "test.txt"
    file, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
    iferr ! =nil {
        t.Fatal(err)
    }
    defer closeResource(file)
    _, err = file.WriteString("wtf")
    iferr ! =nil {
        t.Fatal(err)
    }
}

func closeResource(closeable io.Closer) {
    ifcloseable ! =nil {
        iferr := closeable.Close(); err ! =nil {
            log.Errorln("failed to close resource.", err)
        }
    }
}
Copy the code

Write files through Bufio

The following code:

func TestWriteFileViaBufferIO(t *testing.T) {
    fileName := "test1.txt"
    file, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
    iferr ! =nil {
        t.Fatal(err)
    }
    defer closeResource(file)

    writer := bufio.NewWriter(file)
    defer closeResource(writer)
    _, err = writer.WriteString("just a test")
    iferr ! =nil {
        t.Fatal(err)
    }
    err = writer.Flush()
    iferr ! =nil {
        t.Fatal(err)
    }
}

func closeResource(closeable io.Closer) {
    ifcloseable ! =nil {
        iferr := closeable.Close(); err ! =nil {
            log.Errorln("failed to close resource.", err)
        }
    }
}
Copy the code

Write files through ioutil

The following code:

func TestWriteFileViaIOUtil(t *testing.T) {
    fileName := "test2.txt"
    err := ioutil.WriteFile(fileName, []byte("just a test file."), 0666)
    iferr ! =nil {
        t.Fatal(err)
    }
}
Copy the code

Afterword.

From the examples above, you can see that the ioutil package provides a number of useful methods. If you need to manipulate files, look first to see if the ioutil package provides the corresponding methods.