From the course: Learning Go (2021)
Unlock this course with a free trial
Join today to access over 24,500 courses taught by industry experts.
Write and read local text files - Go Tutorial
From the course: Learning Go (2021)
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…