Problem reading from file with ioutil

Hi! :smiley:
Im new to golang(a week or so), but have some experience in C++

Im trying to create some code that reads from a file that gets constantly written to.
Its should do that by writing the length of the textfile to a integer (the CountLogLines function)
next time it reads the file, it should save each individual new line in a slice of strings,
and then set the length again.
So that afterwards i can access the slice returned from the function and delimit every new line individually.
For some reason my code isnt working tho…
It either outputs nothing, or it skips lines.
Help?
Thanks :slight_smile:

Textfile is written like this:

\n
\r \n
Some_String\n
\n
\r \n
Some_String\n
func countLogLines() int {
	f, err := ioutil.ReadFile(logFileLocation)
	if err != nil {
		log.Fatalf("countLogLines: ioutil.ReadFile() Error: %v", err)
	}
	str := strings.Split(string(f), "\n")
	return len(str)
}

func readLog() []string {
	f, err := ioutil.ReadFile(logFileLocation)
	if err != nil {
		log.Fatalf("readLogFile: ioutil.ReadFile() Error: %v", err)
	}
	str := strings.Split(string(f), "\n")
	if loglen < len(str) {
		var strout []string
		i := 1
		for i+loglen <= len(str) {
			strout = append(strout, str[i+loglen])
		}
		loglen = len(str)
		return strout
	}
	return nil
}
1 Like

Oops…
I was just super tired from lots of coding :sweat_smile:
Turns out that after some rest i was able to solve it in less than 30 minutes.
Sorry for posting prematurely :neutral_face:
Im gonna close the subject ^^

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