hi there serdar jaeger laup here for infoworld idg in this episode of smartgo
we're going to take a look at the way strings are handled and go last episode i talked about the
stringer interface in go which handles conversions of types to strings but then i realized i haven't
said much about strings themselves and how they work and go so let's start with the basics
a definition of what a string is in go in go a string is considered a read-only slice of bytes
and nothing more than that there is no encoding as such in a string it could contain data that could
be interpreted as ascii or utf-8 or what have you but a string in the abstract what we call a string
value does not have an encoding as such it's just a sequence of bytes that's read-only
however a string literal such as a string in ghost source code is stored as utf-8 data within
the string all go source code is interpreted as utf-8 and so that will include strings that are
defined explicitly in your code let's consider the following code snippet as a way to make all
of this a little clearer in this we have two string literals one of them just says hello
and the other one is a little that's made of three emoji and with both of these we're going to print
out the string itself the length of the string its hex representation as a sequence of bytes
and the first element in the string so the first string is about what we would expect it's five
elements long hello and the first element is hex 48 which is the uppercase letter h so far so good
but we need to keep a few things in mind here what we're looking at are not characters they're bytes
it's just that utf-8 text is stored in such a way that basic ascii characters the first 128
elements of the ascii character set like the h here are just stored as is as single bytes
now things will get more complex when we do the same thing to this string of emoji when we print
this data out there are only three emoji in this string but the length of the string is reported as
12 and that's because each of these emoji requires four bytes to encode as utf-8 if we look at the
string as hex we can see all 12 bytes in there and the first element of the string is the single byte
f0 not a character and it's also definitely not the first emoji it's just the first of the four
bytes for that first emoji so this is the first and most important thing to learn about strings
underneath it all they're just read-only slices of byte sequences they can be interpreted as one
kind of encoding or another when we created these strings in our source code they were created as
utf-8 sequences and so that's the way that we should interpret them but the underlying storage
in a string is just a sequence of bytes so what do we do if we want to think about what's inside
a string on an actual character by character basis instead of on a byte by byte basis well go has a
concept for talking about actual characters in a string in go we use the term roon to refer to the
actual characters represented by utf-8 sequences in utf-8 terminology they're called code points
but they came up with the term roon for use in go because roon is easier to say so go has a few
built-in ways to work with rooms the first is that if we use the range object over a string
we don't iterate through the string as a sequence of bytes but as a sequence of runes the string is
decoded into runes one at a time as we go this is a convenient way to iterate through the contents
of a string rune by rune which is something that we're likely to do much more often than iterate
through it bite by byte so in this modified version of our previous code example i'm going
to use range to iterate through our two strings and to print out the utf-8 code point information
for each of these runes in its position in the string as well so with our first string the
runes are only one byte long so the positions in the underlying string are 0 1 2 3 etc but
with the emoji each rune is 4 bytes long so their positions in the underlying string are 0 4 and 8.
but either way we get properly represented utf-8 characters one at a time by using range so this
is a convenient way to go through a string and get each character decoded as we need to by the
way let's try something interesting here let's take a couple of plain ascii characters and
add them onto the end of that string of emoji and see what happens when we rerun this code
they see that we now are seeing single byte ascii characters decoded at positions 12 and 13.
and that's how utf-8 works characters can be anywhere from one to four bytes for the
sake of being backwards compatible with existing ascii text using runes though is how we guarantee
a certain degree of consistency for how each character is taken and processed
another way that we can work with runes natively and go is to take a string and
convert it explicitly into a rune slice and in this third example we're doing exactly that
we print using the original string and then we take our string and convert it into a rune slice
by way of the roon function if we print the rune slice by the way all we get is a string
of numbers each of these is a 32-bit byte each one representing a different character or code point
or rune and we can print each of these characters individually with a string formatting option
or we can convert the whole slice back into a string and print that
by using the string function so to sum up go strings are just read-only slices of bytes
and if you don't need to address individual characters in a string if you're just using
the whole string as is you can do that without worrying too much about what's under the skin
but if you need access to the individual characters themselves not the bytes the
characters are referred to and go as runes and you can iterate through a string with range to
get each rune from a string in turn or you can translate a string into a slice of runes to
work on the whole string at once to access runes individually in future videos i'm going to talk
more about working with runes and strings and i'm also going to talk about using the utf-8 package
for performing more complex processing of utf-8 data that's it for this video if you liked it
leave a comment below don't forget to subscribe to the igg tech talk channel on youtube and for
more smart go and more dev with serdar be sure to follow me on facebook youtube and infowl.com
you