GO writing and reading files with slices

1. How possibly we should work with files using slices

Solving my programming tasks recently, I was figured out that there **are no methods that allowed to get slice of lines and save processed lines on disk. Of course, I could split a strin…


This content originally appeared on DEV Community and was authored by Ushakov Michael

1. How possibly we should work with files using slices

Solving my programming tasks recently, I was figured out that there **are no methods that allowed to get slice of lines and save processed lines on disk. Of course, I could split a string into slice of strings and use that slice, but I would like to have some package once and use it easily, whenever I need it.

Well, I would like to have the following methods:

  1. ReadAllLines that reads file content and return a tuple []string,error
  2. WriteAllLines to write slice of strings
  3. AppendAllLines to add slice of strings to end of existing file.

2.1 New package that meets all requirements

So after I decided what methods such package should have I wrote gfu (gfu is stands for Go File Utils) package and would like to share them with, see github repo:

2.1 ReadAllText method

This method does the following:
1 Returns tuple ([]string, error) of result with auto-detect line ending (CR, LF or CRLF);
2 Removes line ending symbols from slice items
3 Removes empty lines if omitEmpty argument is set to true

Example:

lines, err := gfu.ReadAllLines("myFile.txt", true)

2.2 WriteAllText method

This method does the following:

  1. Inserts the line separator symbol defined in the 3rd argument of WriteAllLines func
  2. Truncates a file if it exists, if not, a file will be created

Example:

lines := []string{
"{",
"    \"id\": 1,",
"    \"name\": \"Michael Ushakov\"",
"}",
}
file := "write_all_lines_test.txt"
err := gfu.WriteAllLines(file, lines, "\n")

2.3 AppendAllText method

WriteAllLines overwrites file content, but what should we do if we need to add some lines portion to an existing file? We should use the AppendAllLines function that, by signature, is identical to WriteAllLines:

lines := []string{
"{",
"    \"id\": 1,",
"    \"name\": \"Michael Ushakov\"",
"}",
}
file := "append_all_lines_test.txt"
err := gfu.WriteAllLines(file, lines, "\n")

additionalLines := []string{
"{",
"    \"id\": 2,",
"    \"name\": \"Alex Petrov\"",
"}",
}

err := gfu.AppendAllLines(file, lines, "\n")

3. Conclusion

All these functions are quite convenient and combined in a small package, also tests were written on all these functions, so we could consider them as reliable. And I go further on my software development journey. don't forget to give us a star if you find this package helpful.


This content originally appeared on DEV Community and was authored by Ushakov Michael


Print Share Comment Cite Upload Translate Updates
APA

Ushakov Michael | Sciencx (2024-11-07T21:10:16+00:00) GO writing and reading files with slices. Retrieved from https://www.scien.cx/2024/11/07/go-writing-and-reading-files-with-slices/

MLA
" » GO writing and reading files with slices." Ushakov Michael | Sciencx - Thursday November 7, 2024, https://www.scien.cx/2024/11/07/go-writing-and-reading-files-with-slices/
HARVARD
Ushakov Michael | Sciencx Thursday November 7, 2024 » GO writing and reading files with slices., viewed ,<https://www.scien.cx/2024/11/07/go-writing-and-reading-files-with-slices/>
VANCOUVER
Ushakov Michael | Sciencx - » GO writing and reading files with slices. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/07/go-writing-and-reading-files-with-slices/
CHICAGO
" » GO writing and reading files with slices." Ushakov Michael | Sciencx - Accessed . https://www.scien.cx/2024/11/07/go-writing-and-reading-files-with-slices/
IEEE
" » GO writing and reading files with slices." Ushakov Michael | Sciencx [Online]. Available: https://www.scien.cx/2024/11/07/go-writing-and-reading-files-with-slices/. [Accessed: ]
rf:citation
» GO writing and reading files with slices | Ushakov Michael | Sciencx | https://www.scien.cx/2024/11/07/go-writing-and-reading-files-with-slices/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.