![]() Since we know that in Go, the strings use the UTF-8 encoding, which means we can have strings that are made up of special symbols as well. Though it might seem that the above approach will work fine for all the strings in Go, unfortunately, it won't. We can also print the individual characters that are present in the string with the help of the %c format specifier. It should also be noted that we used %d format specifier which simply prints the ASCII encoding of a particular byte.ġ15 116 117 100 121 116 111 110 105 103 104 116 46 99 111 109 Printing individual characters of a string The len(str) simply returns the number of bytes that are present in the string. In the above example, we are iterating over the string using a for loop, and the len(str) is used to limit the for loop. We can access each byte of a string, a string is simply a slice of bytes.Ĭonsider the example shown below where we have a function named PrintEachByte(), which is used to print every byte that is present in a given string. The space occupied by strings and text files in Go is less because of the variable-width characters.As UTF-8 is the standard in Go for encoding, there's no need for Go to encode and decode strings like other languages.Go strings possess some advantages over strings from other languages. It is different from other programming languages, where the size of the strings is usually fixed. The size can vary from 1 byte to 4 bytes. One important point to note is that in Go, the strings are a sequence of variable-width characters. Now, we know a little bit about a string in Go, let's explore an example where we will create a simple string and then print it using the fmt.Println() function. In Go, the string data type can reserve up to 4 bytes of characters if the string uses different characters, but if the string is only an ASCII character, Go is smart enough to reserve only one byte of space. In Go, strings are created by making the characters enclosed inside double quotes "". ![]() These files mainly include XML files, JSON strings, text files, etc. The UTF-8 encoding is a very widely used encoding, as it is the standard encoding for different types of files. It is sometimes also known as a sequence of characters where the characters follow the UTF-8 encoding. A String in Go is simply a slice of bytes.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |