From the course: Learning Go

Write and read local text files - Go Tutorial

From the course: Learning Go

Write and read local text files

- [Instructor] Now that we've learned a bit of the language itself let's take a look at a couple of common solutions. There are a number of ways to create a file in Go. There are high level abstractions of the low level operations that are required. I'm going to show you a very simple approach to creating and reading text files. I'll start in my import statement, where I'll add three more declarations: io, io/ioutil, and os. Next, I'm going to be doing a lot of error checking. So to make this a little bit easier, I'll create a new function that I'll call checkError. It'll receive an error object and I'll examine the error object. If err doesn't equal nil, then panic and display the error message. So now I can check the errors whenever I want to. Now I'll go back to my main function. I'll get rid of this output. And instead, I'm going to create a string. I'll call it content, and I'll set it to a literal string of 'Hello from Go!' And my goal is to take that string and write it to a file. My next step is to create a file reference. I'll create a variable that will reference a file. I'll just call it file. And I'll be calling a function that can return an error, so I'll also receive the error object. And I'll set that with os.Create. And then I'll pass in the name and location of the file. I'll start with dot slash, meaning in the same directory as the application, and then fromString.txt. You can name the file anything you like. Now I'll use that checkError function, just to make sure that everything's okay and I can continue. If there's a problem, it'll crash the application. Now I'm ready to actually write to the file. I'll create another variable that I'll call length and another error object. And I'll get those variables with this expression io.WriteString. The right string function requires a writer object and a string. I'll pass in the file as the writer and my content variable. And then once again, I'll check the error. And if I get this far, then I know that I've created the file. The length variable will be returned by write string, and it'll be the number of characters that were written to the file. So now I'll say fmt.Printf, and then I'll put a message of 'Wrote a file with', and then a placeholder, and then my line feed at the end, and then I'll pass in the length variable. And finally, I need to close the file. And I'll introduce a new keyword that we haven't seen before. Defer. This means, wait until everything else is done and then execute this command. And I'll call file.Close. Whenever you're working with files it's very important to close them when you're done. I'll save those changes and run the application. And I see a message "Wrote a file with 14 characters." And I also see this new file has been created in the same directory as my application. And I can open it and see the string that I wrote to it. Now let's read from that same file. I'll put this code in a separate function. I'll name the function readFile. And it's going to receive a file name as a string. When you read a file, it always comes to you as an array of bytes. I'm just going to call this returned value data. Once again, I'll have to check for an error. And this time I'll call ioutil.ReadFile. And I'll pass in that file name. I'll check the error. And as before, if there is an error it'll crash the application. And then I'll call fmt.PrintLine. I'll put in a label of text read from file. And then I'll pass in the data but before I do, I'll convert it to a string using the string function. Now I'll go back up here. And once again, I want to defer this command because I want to wait until everything else is done before I try to read the file. And I'll call the readFile function. I need to pass in the name of the file I'm reading, dot slash fromString.txt. I'll run the code. And I see that I'm successfully reading from that same file. To confirm that everything's working, I'll delete that file and then run the code again. And I see once again that everything is working perfectly. It's very important to use this defer keyword whenever you're working with anything that might not run automatically in the current thread. You want to wait until the file is completely closed before you try to read it. And that's what the defer keyword does. So that's how simple it is to create and read text files using Go.

Contents