Find ASCII Value of a Character in Go
In this tutorial, we will learn how to find the ASCII value of a character in Go. We will cover the basic concept of ASCII values and implement a function to get the ASCII value of a given character.
What is an ASCII Value
ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns a numerical value to each character. For example, the ASCII value of 'A' is 65 and 'a' is 97.
Syntax
The syntax to find the ASCII value of a character in Go is:
func asciiValue(char rune) int {
return int(char)
}
Example 1: Finding the ASCII value of a character
We can create a function to find the ASCII value of a given character by converting it to an integer.
For example,
- Define a function named
asciiValue
that takes one parameterchar
of typerune
. - Convert the character to an integer using
int(char)
. - Return the integer value, which represents the ASCII value of the character.
- In the main function, call the
asciiValue
function with a sample character and print the result.
Go Program
package main
import (
"fmt"
)
func asciiValue(char rune) int {
return int(char)
}
func main() {
// Sample character
char := 'A'
// Find the ASCII value of the character
result := asciiValue(char)
// Print the result
fmt.Printf("ASCII value of '%c' is %d\n", char, result)
}
Output
ASCII value of 'A' is 65