How Can I Find Out The Number Of Empty Line On File

Hello

I want to find empty line on file
e.g.
Favorman.txt contains

First Line: Hello
Second Line: // Empty Line
Third Line : Everybody
Fourth Line : // Empty Line
Fifth Line : Its me

How can I find out the number of empty lines

arquivo.txt

First line
Second line
Third line

Third line

Third line

Third line

Third line

Third line

Third line

main.go

package main

import (
	"os"
	"io/ioutil"
	"strings"
	"fmt"
	"strconv"
)

func main() {
	fileIO, err := os.OpenFile("arquivo.txt", os.O_RDWR, 0600)
	if err != nil {
		panic(err)
	}
	defer fileIO.Close()
	rawBytes, err := ioutil.ReadAll(fileIO)
	if err != nil {
		panic(err)
	}
	countLines := 0
	lines := strings.Split(string(rawBytes), "\n")
	for _, line := range lines {
		if line == "" {
			countLines += 1
		}
	}
	fmt.Println("Total: " + strconv.Itoa(countLines))
}

Again thanks , you are my hero:)

we’re welcome, :slight_smile:

https://play.golang.org/p/dwYZTQ4qDs

I noticed that you also posted this same question to golang-nuts after receiving your answer here. This is considered poor etiquette. Please try not to do this in the future.

1 Like

They are not same questions . Look at one more times before you judge me.

https://groups.google.com/forum/#!topic/golang-nuts/ZrlDjqkxcWs

Is this your question?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.