Strings and Runes in Go [Go for Beginners #5]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so what is a string in go a string in go is a sequence of any valid udf 8 characters we have a simple example on the screen on line number 10 we have the string a string in go with spaces in between and when we try to run this small piece of code we see that the value of our string is a string in go with spaces in between and the type is string so let me show you an interesting result in the mean time so i print so i have printed the string here now let me repeat the print statement and say s at 2 and s add 2 again if i run this small piece of code i see that a value of 115 gets printed and the type is uint 18 now this shows us two things uh the first one being that we can access the characters of a string using this array like notation so when i say s of two i get the third character the string s because the indexing within the string starts from 0 so s of 0 would be a s of 1 would be the space and s of 2 is this character s but why are we actually seeing a value of 115 here and a type of u and 18 instead of the character s so why this happens is that internally in go strings are just a collection of bytes and bytes in go are just an alias of type unsigned int 8 so when we try to extract a single character from a string in go we get the byte or the unsigned in representation of that character now what if we actually did want the character to be printed out here well no worries we can just convert the byte representation into a string representation and um let me just do that here as well and when i go ahead and run this [Music] we see the value of s and the type of string and a very important thing to note here is that although go will allow you to read characters from a string with this format it will not allow you to write characters into a string with the same format so if i say something like s of 1 is equal to u and try to run this um it says it cannot assign to s of one and it will not work even if i try to assign a byte to s of one and it again says cannot assign to s one that is because strings in go are immutable now we just saw an example where we converted a byte into a string let us now try to see an example where we convert a string into a collection of bytes to do that i am going to declare a variable called b and then i am going to initialize it with a byte slice and i am going to give it the argument of s when i run this okay we did not use b somewhere so let me go ahead and simply print out b so we'll use the same print statement that we have been using which will print both the value as well as the type of b and when i run this okay we see that the variable b is a long collection of numbers or you can say bytes and its type is a collection so this square bracket means a collection or an array which will furth which we'll see in one of our further videos so a collection of unsigned int 8 or byte and what are these values well these values represent the ascii or the udf values for each of the characters within this string so we have a which okay we have 97 which represents an a 32 which represents a space then 115 which represents an s and so on so if you see um we have we had a space and 32 here so similarly we have a 32 here representing this space and a 32 here as well representing this space um also you can see that we had a 103 here and 103 here so those represent this g as well as this g now where might we need something like this byte slice that we just declared here well a lot of functions in go uh don't work with strings but are capable of working with byte slices so suppose for example we want to send a response from our web server to the user at one time we want to send a string and at another time we want to send a file so both the string as well as the file can be converted to a byte slice and then we will be able to make use of the same function to send that byte slice to a particular user so i hope that the intent is clear and the last data type that we are going to see is called a roon data type so what is the difference between a string and a rune well a string contains many characters whereas roon will contain only one characters and where a string can contain only characters which are valid udf 8 arun can contain any character which is a valid udf 32 so suppose i say var s rune is equal to suppose let's say t and i run this i see that the value of s is 116 and its type is in 32 again a little mind boggling right we declared s as a room but when we type when we try to print the type of s we see in 32. now why is this happening well this is happening because the roon is just simply an alias for a 32-bit integer so for the meantime i'd simply like to conclude our discussion about string and ruin here itself all of the code that we just saw has been updated in this github repository ae dorado slash learning go so do check it out over here if you found the video helpful please do hit the like button if you find the content of my channel helpful please click subscribe you can hit the bell icon to never miss any new updates and like always thanks a lot for watching and i'll see you very soon in a brand new tutorial
Info
Channel: Codetuber
Views: 917
Rating: undefined out of 5
Keywords:
Id: XKyFc2fZuIg
Channel Id: undefined
Length: 6min 37sec (397 seconds)
Published: Fri Dec 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.