ivanzoid
9/8/2018 - 11:50 AM

Golang read first string from file.go

package main

import (
	"os"
	"bufio"
)

func readFirstLineFromFile(path string) (string, error) {
	file, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	scanner.Split(bufio.ScanLines)

	scanner.Scan()
	return scanner.Text(), nil
}