Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Read array of byte until new line – golang

I want to read a file from S3. This file contains several lines, and In each line, there is a string.

something like:

Alice
Bob
Jack

I have an API that reads my file, but the output is: []byte

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I split the byte array by a new line and convert them to string? My Desire output is an array of strings.

>Solution :

To create a slice of strings from a slice of bytes, convert the slice of bytes to a string and split the string on newline:

 lines := strings.Split(string(fileContents), "\n")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading