Skip to content
Home » How to get the ASCII value of a character in Golang

How to get the ASCII value of a character in Golang

Learn about How to get the ASCII value of a character in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

How to get the ASCII value of a character in Golang

// Golang program to get the ASCII value of a character.

package main
import "fmt"

func main() {
    //Declare a character type variable.
    var val byte = 'C'
    
    fmt.Printf("Character value: %c",val) 
    fmt.Printf("\nASCII value: %d",val) 
}

In the above program, we declare the package main. The main package instructs the Go language compiler that the package must be compiled and an executable file generated. We imported the fmt package, which contains the files of the fmt package, and then we can use a function related to the fmt package. The main() function is the entry point for the program. Here, we created a variable val of byte type, which is initialized with the ‘C’ value. After that printed character and ASCII value of variable val using Printf() function on the console screen

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
How to read and print an integer variable in Golang
Go program to find the average of three numbers