Read and Write files in Golang #golang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so in this video we are going to learn how to write and read files and we'll also see if a file doesn't exist so how it behaves so let's start with the writing the file so we are going to use the OS package so let's see os. WR file okay so this is the function which we are going to use so it takes the file name okay the data in bite format and the permission of the file like uh once you are whatever file you're writing so what will going to be its permissions and all okay so let's have a message or let's call it data okay it going to we by type and our content going to be suppose practice code okay and uh let's see what are the types available uh sorry the file mode so these are the file modes available almost 10 okay so we are going to use this 07 uh which is kind of a uh have the all the permission like read write and execute for all the users and groups and everyone okay and if the file already exists and you only want this that particular file to be written only not the read one so that time you can only give the 02 so we going to see all these things so let's first uh give the file name uh so it's going to be test.txt okay uh the next going to be the data and the last one going to be 07 the file permission it will return the error so let's handle it uh if error is not equals to n that time just give a fattal error and uh if everything is well let's print it like a success okay so let's just see corun main. and you can see uh it is success and if we'll see file okay test.txt file is created and if you see it is like practice go everything is okay so now let's just see if we just change the uh permissions okay so this time we'll use the 02 so 02 only allow the right uh and nothing else so if we'll just see again gun. go okay uh just refresh it yeah so file is there but if we if I'll try to open it that time I'll get error like there is no permission to read okay now let's move to the read operation so let's just first uh create it again okay uh using 07 okay uh go R main. go with all the permission like read write everything okay so we can read it and Os do read file and here also we can either give the complete file path uh the complete file path you can copy okay or the file name if you are in the same directory so we are in the same directory so we going to use test.txt okay and as we can see it is returning the bite and error so we have the bite and the error uh let's first handle the error error if is not equals to nil log. FAL error and the content is in the bite so we have to convert it into a string to read it print Ln string F go run me. go yeah practice go okay so this is how we read and write files so this particular file can be anything so we have completed the right and read operation now let's jump to the upend operation like when the file is already there okay so we already have a file and it has certain content in it so how we can do it uh using two ways we can do it so the first going to be uh read and write okay so how we are going to do is like suppose we have this thing so let's just copy it paste it here so this is our content which is in the bite format okay and let's suppose we have another message which is like uh appended message okay uh let's have an enter also and convert it into a bite format and now our this bite is in the bite format so we can append it so what we can do is like b y = to append message okay so in this way uh our bite is now appended to the uh whatever we read okay and now what we can do uh we can simply write it okay test.txt data instead data it should be by okay and now I will just go run m. and if you'll see see appended message is there so in this way so this is the first way in the second method what we can do open the file open and then write okay so for this uh we are going to use os. open file okay uh the file name so file name going to be test.txt okay and we can see the flag so here we have to give multiple Flags so these flags are like OS do we want to append in it okay so there is one flag called append okay then another flag we have to give is like os. create uh this is like a optional thing and Os do write only okay and uh then the third flag or like the permission of the file going to be 07 which we already using okay and uh then it will return a file okay so this is our file and some error okay so let's just handle that error now we have the file before proceeding uh we should close it okay we'll have another message white type it's like another appended message file. write and the bite and it is returning the number of bytes it kind of return in it okay uh into the file so that we can ignore and we only required the error okay so we'll just handle the error as like if error is not equals to nil that time just FAL okay let's run it go run men. go it worked I guess yeah another appended message so I hope you like this plexure uh in the next lecture I will try to cover the Json like how to read and write the Json into to a file
Info
Channel: practicego
Views: 331
Rating: undefined out of 5
Keywords: golang, golang tutorial, learn golang, golang for beginners, golang api, gorilla mux, mongodb, web development, backend development, rest api, api development, golang web app, golang crud operations, gorilla mux tutorial, deploy golang app, coding, programming, software development, tech, tutorial, beginner, fullstack, freecodecamp, goroutine, interview, interviewquestion, 2024, docker, containerise, optimise, file, read, write
Id: lkeh0TRSBfo
Channel Id: undefined
Length: 7min 6sec (426 seconds)
Published: Thu Apr 25 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.