In this section, we're going to see how to create a temporary file. Let's also have a variable that contains a string called helloWorld := "Hello, World"
. We're going to use ioutil
package and it provides the TempFile()
method. The first parameter is directory; if you don't pass anything to it, it will use its default temporary directory, which we will be using in this case, and the second one is to give a prefix to your temporary file, which will be hello_world_temp
. It returns two things: the first one is the temporary file that is created and the second one is the error (err
). Now, if any error occurs, then we are going to panic and we'll throw the error as the message.
When you're done with the temporary file, what's recommended is you remove the file, and we can use the defer
function where we have a os.Remove()
method. You just have to provide the name of the file and it will find it and remove it. Now we're going to write the helloWorld
into our file. Let...