Updated April 14, 2023
Introduction to Golang Rune
Run in the go language a way of an intermediary, because we know there are large number of people and languages are available like you have seen various punctuation symbol, so we need something which unify all of them and any one car write in any way still it will work for others, so basically reflect module to see the Unicode of the any punctuation of letter for example if we are writing ‘/n’,’/r’ etc like punctuation’s then we need to have some uniform way also so that others can be able to use it, runs is a superset of the ASCII and they have almost all the characters of the world.
Syntax
Below is a simple syntax for the where we show how we can create a run, so basically it is simply assigning the characters or punctuation to the run variable. We can explain the below syntax in the following steps .
- First, we have taken a variable run, here you can give any name to this variable and assign them values.
- Next, we get the Unicode of the run variable. Simply with the command reflect.Type(run).
- Remember the reflect is the package which we need to import to see the Unicode value of the run, or of the characters.
Please see the below syntax for understanding.
run := symbol of punctuation
reflect.TypeOf(run)
How Does Rune Work in Go language?
So before understanding the working of the runs we need to understand why it is being introduced, actually there are many languages and punctuation’s so it plays a role of the intermediate which allow many languages to work and a unique code for every language so that it will be easily understandable in a common way for all other languages. We can discuss the working of the runs in the following steps.
- When we write any string with more than one character it will not work with that, as it is made for single characters.
- We can see an example of UTF-8 which encodes the all Unicode for 1 to 4 bytes and here out of the 4 bytes 1 byte will be used for the ASCII characters and remaining will be used for the runs.
- We should be aware that every ASCII contains a total of 256 attributes. Out of these 256 characters, 128 characters are the numbers from 0-127 these are reserved for them.
- We do not have to do anything to perform the runs it manages internally, which Unicode generation is there internally, if anyone wishes to see the Unicode for any characters they can see by using reflect package of the go language. Now to use the reflect package of the go language we need to import the reflect.
- We will see that out of the reflect package that every character has a unique value and that unique value is Unicode, this code will be the same for all the languages so that everyone can use them without hesitation and thinking as an issue for the other people to use this in their languages.
- Once we assign some value of character or punctuation to any variable there will be a Unicode for that will also be there and that uncode will be similar for other environments which gives flexibility.
Examples to Implement Golang Rune
In the below, we have given some of the examples for the string and numeric and symbols type characters and we were tried to print the value of them. In case if we want to execute these examples then we can create a file with the name of runs.go and copy and paste the examples of the below codes and run the command go run runs.go. We can even create file any name according to our requirements.
Example #1
Below is an example where we are working with the runs generated with the character and we are printing the output of every character and their Unicode also. We are also printing the type of character.
Code:
package main
import (
"fmt"
"reflect"
)
func main() {
code1 := 'B'
code2 := 'g'
code3 :='\a'
fmt.Printf("The actual code is : %c; The Unicode is: %U; Type: %s", code1,
code1, reflect.TypeOf(code1))
fmt.Printf("\nThe actual code is : %c; The Unicode is: %U; Type: %s", code2,
code2, reflect.TypeOf(code2))
fmt.Printf("\nThe actual code is : The Unicode is: %U; Type: %s", code3, reflect.TypeOf(code3))
}
Output:
Example #2
Below is an example where we are working with the runs generated with the numeric and we are printing the output of every numeric value and their Unicode also. We are also printing the type of numeric.
Code:
package main
import (
"fmt"
"reflect"
)
func main() {
r1:= '1'
r2:= '2'
r3:='3'
fmt.Printf("The actual code is : %c; The Unicode is: %U; Type: %s", r1,
r1, reflect.TypeOf(r1))
fmt.Printf("\nThe actual code is : %c; The Unicode is: %U; Type: %s", r2,
r2, reflect.TypeOf(r2))
fmt.Printf("\nThe actual code is : The Unicode is: %U; Type: %s", r3, reflect.TypeOf(r3))
}
Output:
Example #3
Below is an example where we are working with the runs generated with the symbols and we are printing the output of every symbol and their c also. We are also printing the type of symbol.
Code:
package main
import (
"fmt"
"reflect"
)
func main() {
r1:= '*'
r2:= '#'
r3:='&'
fmt.Printf("The actual code is : %c; The Unicode is: %U; Type: %s", r1,
r1, reflect.TypeOf(r1))
fmt.Printf("\nThe actual code is : %c; The Unicode is: %U; Type: %s", r2,
r2, reflect.TypeOf(r2))
fmt.Printf("\nThe actual code is : The Unicode is: %U; Type: %s", r3, reflect.TypeOf(r3))
}
Output:
Conclusion
From this tutorial, we learned the basic concept of the runs in the go language and we learned the working and its syntax. We also focus on some of its examples for understanding the runs for the character’s number and for the symbols.
Recommended Article
This is a guide to Golang Rune. Here we discuss the basic concept of the Golang Rune and its syntax along with the help of some useful examples and Code Implementation. You can also go through our other suggested articles to learn more –