Golang io package interfaces & functions (io.Reader, io.Writer & io.Seeker...)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you guys can probably feel related at least a little bit when i'm using functions like f skin and f prints coming from the fonts package i've always just passed in the parameters that these functions are asking for without a questioning how everything works under the hood more specifically how these i o readers and i o riders work under the hood and additionally how the i o package in general in go works so i finally did a little bit of a digging and here's what i found [Music] some ninjas welcome back to the golang dojo your number one resource for all things go make sure to get your free golang cheat sheet at golangdojo.com in this video we are talking about the i o package the i o package provides a basic interfaces for input output operations more specifically we will be talking about a writer reader seeker interfaces and a list of functions working alongside with these interfaces the first one we'll talk about is the writer interface which wraps the basic write method as you can see right here now let's say that we are creating this file of a file.txt and after some instructions in between we will go ahead and close it as a file to get the writer interface object we will need to use the io package writer and pass into the file this will return a writer interface object let's go ahead and write through this file using the writer that we created there's a function that will return number of bytes written into the file and error if there is any error run this program as you can see five bytes have been written to the file and there is not any error if we do ls we can see a file is indeed created and if we open that file we can see hello right here alternatively we can use the write string function pass in the writer and the string that we want to write this will also return number of bytes written and error if there is any error go ahead and run this program again as you can see we are writing one single byte because there is only one single character let's go ahead and open the file.txt file file we can see that the character is indeed written to the file all right next we have the reader interface which wraps at the basic reading method as you can see right here this time instead of creating another file we will open the file that we've previously created and put it into similarly to create a reader we just call io.reader and pass into the file that we opened next let's go ahead and read this file using the reader we created as you can see it's asking us to pass in a slice of a byte which is the buffer we will use to read this file so let's create that buffer size of a 10 should be enough because remember we've written hello and a exclamation mark into the file so that's six bytes so size of 10 would be more than enough there's a reader method will also return number of bytes read and error if there is any then we'll print out a number of bytes read if there's any error and the buffer that we use to read the file let's go ahead and run this program as you can see we've read the six bytes no error and the buffer is hello now notice that there is some there are some spaces here because the buffer size is of size 10. now you might be thinking what if there are more bytes than just a 10 in the file that we are trying to read let's say that the buffer is actually smaller than the total number of a byte of information inside of a data file we are trying to read we can copy and paste the exact same code but this time we are using the mini buffer of a size of one instead of a 10. if we run this program again we would see we only see the first advisor being printed out because the mini buffer is only of a size of one so in order to read everything in the file we need to replace these two lines of instruction with some sort of for loop with some instructions in it in order to read into the file until it's at the end of the file and the changes are pretty straightforward all we need is there's a for loop to keep on the reading until there is an error coming from there's a read method and then we just break out if there is an error let's go ahead and run this program again as you can see we are seeing how low being printed out character by character on each iteration of the for loop reading using there is a mini buffer of a size one you can also see that the error that is breaking the for loop is this end of file error and number of bytes of red on that iteration is a zero because there is nothing left to read at the end of the file alternatively again we can also use the read all function to read everything inside of the file instead of having to iterate through or having to pick the size of the buffer to read the file reading the file this way will return as a buffer instead of the number of bytes of red under there is a read operation and the size of this buffer will be the number of a byte of information inside of a data file that said you probably don't want to use this function if the size of the file is a gigantic because you will also have a buffer that is a gigantic which may take up too much memory during run time but as you can see the real function is a working and we are seeing the content of the file being printed out right here next let's talk about the seeker interface which wraps the basic seek method as you can see right here so here is the scenario let's say that we have the exact same instructions for when we are trying to use the read all function to read the file this is great however if we try to copy and paste these two lines of instruction which means we are trying to read the entirety of the file twice and then try to run this program again as you can see the last read all is actually not going to work and nothing is being copied uh being read to the buffer right here that is because the reader has already reached the end of the file after this line of instruction when we are trying to read the entirety of the file for the second time it is reading starting at the end of the file that's why we are not seeing anything being read to fix this we can use the seeker interface and we can convert the reader into of a type sql interface instead on this line of instruction and then we see get back into the stars of the file and the offset is going to be zero now if we try to run this program again you can see we are seeing the entirety of the file being printed out again additionally we can play around with these two values that we are passing in uh we can do i o seek end of the file and we can pass in negative 5 for example as the offset so if we try to run this program again as you can see it is going to print out the last five bytes of information that is in the file we can also change this to seek current which is the current point of the reader in the file and this should print out the exact same thing because the reader has reached at the end of the file so the seeker current is just the end of the file and we are grabbing um the five byte previous uh previous to the current spot of the reader which is the last of five bytes of information in the data file so these are the interfaces that you likely encounter when using the io package and the list of functions that are typically work alongside with these interfaces that said if you have any questions please feel free to leave a comment down below and make sure to get your free her go shishi's out of golangdojo.com and make sure to like and subscribe and i'll see you ninjas in the next video [Music] you
Info
Channel: Golang Dojo
Views: 11,764
Rating: undefined out of 5
Keywords: golang, learn golang, go lang, go language, golang tutorial, go programming, golang for beginners, golang 2022, golang io package, golang package io, golang io, go io, golang io reader, golang io reader writer, golang io writer, golang io seeker, go io seeker, go io writer, go io reader, golang seeker, golang writer, golang reader, io package go, io package in golang, io package in go, io writer golang, io reader golang, io seeker golang, seeker golang, writer golang
Id: N1wSFA6VNuM
Channel Id: undefined
Length: 10min 39sec (639 seconds)
Published: Thu Mar 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.