Skip to content

Instantly share code, notes, and snippets.

@scriptkkiddie
Created March 1, 2023 02:41
Show Gist options
  • Save scriptkkiddie/9256fe1590cb10c9f9b25f8fe4524a0a to your computer and use it in GitHub Desktop.
Save scriptkkiddie/9256fe1590cb10c9f9b25f8fe4524a0a to your computer and use it in GitHub Desktop.
File Reader Prototype in Golang
package main

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

func main() {
	// Open the file
	file, err := os.Open("filename.txt")
	if err != nil {
		log.Fatalf("Error opening file: %s", err)
	}
	defer file.Close()

	// Create a scanner to read the file line by line
	scanner := bufio.NewScanner(file)

	// Iterate over each line in the file
	for scanner.Scan() {
		line := scanner.Text()
		println(line)
		// print(line)
		// log.Println(line)
	}

	// Check for errors while scanning the file
	if err := scanner.Err(); err != nil {
		log.Fatalf("Error reading file: %s", err)
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment