Go (Golang) / File Handling and I/O Operations

Using bufio for Buffered I/O Operations

In this tutorial, we'll delve into using the 'bufio' package for buffered I/O operations in Go. We'll learn how to read from or write to physical disk with a buffer, improving I/O…

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Explains how to read and write files, work with directories, and handle I/O operations in Go.

Introduction

The goal of this tutorial is to provide a comprehensive understanding of the bufio package in Go, which allows for buffered I/O operations. Buffered I/O operations are more efficient because they reduce the number of system calls made by reading from or writing to a buffer in memory, rather than directly from or to a disk.

From this tutorial, you will learn:

  • What Buffered I/O is and its advantages
  • How to use the bufio package in Go for reading and writing operations
  • Best practices when using buffered I/O

Prerequisites: Basic understanding of Go programming and file operations is required.

Step-by-Step Guide

The bufio package in Go provides buffered I/O. It wraps an io.Reader or io.Writer object, creating another object (Reader or Writer) that also implements the interface but provides buffering and some help for textual I/O.

Reading with bufio

To read a file in Go, we can use the bufio.NewReader function. This function takes an io.Reader object, creates a new Reader whose buffer has the default size, and returns it.

file, err := os.Open("test.txt") // For read access.
if err != nil {
    log.Fatal(err)
}
defer file.Close()

reader := bufio.NewReader(file)

In this example, we open a file and create a new buffered reader using the bufio.NewReader function.

Writing with bufio

For writing, we can use the bufio.NewWriter function. This function takes an io.Writer object, creates a new Writer whose buffer has the default size, and returns it.

file, err := os.Create("test.txt")
if err != nil {
    log.Fatal(err)
}
defer file.Close()

writer := bufio.NewWriter(file)
_, err = writer.WriteString("buffered\n") // Write a string
if err != nil {
    log.Fatal(err)
}

writer.Flush() // Don't forget to flush!

In the writing example, we create a file, write a string to the file using the WriteString function, and then flush the buffer with the Flush function.

Code Examples

Example 1: Reading a File with bufio

package main

import (
    "fmt"
    "log"
    "bufio"
    "os"
)

func main() {
    file, err := os.Open("test.txt") // Open the file for read access.
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file) // Create a new Scanner for the file.

    for scanner.Scan() { // Read line by line
        fmt.Println(scanner.Text()) // Print the line.
    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err) // If there was any error while reading, log it.
    }
}

In this example, we are using bufio.NewScanner to read a file line by line and print each line. scanner.Scan() reads the next line and removes the newline character from the end. scanner.Text() returns the current line as a string.

Example 2: Writing to a File with bufio

package main

import (
    "log"
    "bufio"
    "os"
)

func main() {
    file, err := os.Create("test.txt") // Create the file for write access.
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    writer := bufio.NewWriter(file) // Create a new Writer for the file.
    _, err = writer.WriteString("Hello, Gophers!") // Write a string
    if err != nil {
        log.Fatal(err)
    }

    writer.Flush() // Don't forget to flush!
}

In this example, we write a string to a file using bufio.NewWriter. writer.WriteString writes a string to the writer, and writer.Flush writes any buffered data to the underlying io.Writer.

Summary

In this tutorial, we covered the use of the bufio package in Go for buffered I/O operations. We learned how to read from and write to files using bufio, and looked at examples of both. The next step is to practice these concepts through exercises, and then try to apply them in real-world scenarios.

Practice Exercises

Exercise 1: Read a file and count the number of lines.

Exercise 2: Write a program that takes user input, stores it in a buffer, and then writes it to a file when the user enters 'exit'.

Exercise 3: Modify the program from Exercise 2 so that it writes to the file after every line of input, but still uses buffered I/O.

Solutions and Explanations

The solutions to these exercises can be found here. They provide a good starting point for understanding how to use the bufio package in various scenarios. Remember, the goal is not just to solve the exercises, but to understand how and why the solution works.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Image Compressor

Reduce image file sizes while maintaining quality.

Use tool

Time Zone Converter

Convert time between different time zones.

Use tool

File Size Checker

Check the size of uploaded files.

Use tool

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help