work with files — Text files — Append text to a file

Appending text to a file in Go can be done using the os package. The os.OpenFile function provides the capability to open a file with specific flags, including the os.O_APPEND flag for appending data to the end of the file.

Example: Appending Text to a File

Let's create an example where we define a function to append text to a file.

Step 1: Import Necessary Packages

First, import the necessary package.

package main

import (
    "fmt"
    "os"
)

Step 2: Define the Function to Append Text to a File

Create a function that takes a file path and a string of text as inputs and appends the text to the file.

// Function to append text to a file
func appendTextToFile(filePath string, text string) error {
    // Open the file in append mode
    file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
    if err != nil {
        return err
    }
    defer file.Close()

    // Write the text to the file
    _, err = file.WriteString(text)
    if err != nil {
        return err
    }

    fmt.Printf("Successfully appended text to file: %s\\\\n", filePath)
    return nil
}

Step 3: Use the Function

Finally, use the appendTextToFile function in the main function to append text to a file.

func main() {
    // File path to append to
    filePath := "example.txt"

    // Text to append to the file
    text := "This is the appended text.\\\\n"

    // Append the text to the file
    err := appendTextToFile(filePath, text)
    if err != nil {
        fmt.Printf("Failed to append text to file: %v\\\\n", err)
        return
    }
}

Explanation

  1. Import Necessary Packages:
  2. Define the appendTextToFile Function:
  3. Use the Function in main:

Detailed Output

The program will append the specified text to example.txt and print the result. For example, if example.txt initially contains:

Hello, world!
This is an example file.