From the course: Learning the Go Standard Library
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Reading files - Go Tutorial
From the course: Learning the Go Standard Library
Reading files
- [Instructor] In this example, now we're going to see how to read files, using the Go package library, and just like with the writing process, the ioutil package provides a convenience function for reading an entire file into memory in one operation. So that's where we're going to start. So here in chapter four, in the start folder, let's open up read_start, and I'm going to import the modules that I need. So, let's uncomment ioutil, and let's also uncomment the handle error function, we're going to be needing that, all right. So first, just as I did for the writing example, I'm going to create two variables, content and err, and I'm going to use the ioutil package, but instead of writing, I'm going to call the ReadFile function, and I'm going to read my sampletext.txt file, and you can see that that's this file right here, all right, and I'll call handleErr in case there's an error. So at this point, the content variable…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.