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,

  1. Define a function named asciiValue that takes one parameter char of type rune.
  2. Convert the character to an integer using int(char).
  3. Return the integer value, which represents the ASCII value of the character.
  4. 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