Skip to content

Instantly share code, notes, and snippets.

@misterunix
Last active February 12, 2023 00:20
Show Gist options
  • Save misterunix/9244d88f3d88d58a3897e19b19c4fc31 to your computer and use it in GitHub Desktop.
Save misterunix/9244d88f3d88d58a3897e19b19c4fc31 to your computer and use it in GitHub Desktop.
Read a gzip file

load a gzip compressed file in to a slice, line by line.

package main

import (
	"bufio"
	"compress/gzip"
	"log"
	"os"
)

func readGzipLines(path string) ([]string, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}

	gz, err := gzip.NewReader(file)

	if err != nil {
		log.Fatal(err)
	}

	defer file.Close()
	defer gz.Close()

	scanner := bufio.NewScanner(gz)
	var lines []string
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}
	return lines, scanner.Err()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment