Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - FILES


We all have came across handling files in some way. Be it a '.txt' file or a '.dat' or even a '.go' file that we use to write our Go Programmes.


So, files are something, where we store some information.


Say for example, we want to write the below paragraph to a file.


In a huge pond,
there lived many fish.
They were arrogant and
never listened to anyone.

So, we want to store the four lines in a File and name the File as 'myfirstfile.txt'.


And in general, you need to follow the below steps create the file and write the lines :

  1. You need to open the File in a Notepad or TextEdit and name the file as 'myfirstfile.txt'.
  2. Then you need to write the above four lines.
  3. And close the file once done.

Again, if you want to make some changes to the file(Say adding or deleting a few lines) :

  1. You need to open the existing File 'myfirstfile.txt' in a Notepad or TextEdit.
  2. Make some changes to it.
  3. Close the file.

Similarly, if you want to delete the 'myfirstfile.txt' :

  1. You go to that file and delete it.

Now, let us corelate, how can we achieve the same in Go.


So, to both read and write to a file , Go provides the io/ioutil module.


Next, let us see, how can we write the above lines to a file named 'myfirstfile.txt'.