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.
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.
The syntax to find the ASCII value of a character in Go is:
func asciiValue(char rune) int {
return int(char)
}
We can create a function to find the ASCII value of a given character by converting it to an integer.
For example,
asciiValue
that takes one parameter char
of type rune
.int(char)
.asciiValue
function with a sample character and print the result.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)
}
ASCII value of 'A' is 65