The Python ord() function is a built-in function that returns the Unicode code point of a given character. It takes a single argument, which can be a string of length 1 or a Unicode character. The returned value is an integer representing the Unicode code point of the character. The ord() function is useful when working with Unicode strings and characters, as it allows you to convert characters to their corresponding code points, which can then be used for various operations such as sorting, searching, and encoding. Keep reading below to learn how to python ord in Go.

Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Buy Now On Amazon

Python ‘ord’ in Go With Example Code

Python’s `ord()` function returns the Unicode code point of a given character. In Go, we can achieve the same functionality using the `fmt.Printf()` function and the `%U` verb.

Here’s an example code snippet that demonstrates how to use the `fmt.Printf()` function to get the Unicode code point of a character in Go:


package main

import "fmt"

func main() {
char := 'A'
fmt.Printf("%U", char)
}

In this example, we define a variable `char` with the value `’A’`. We then use the `fmt.Printf()` function with the `%U` verb to print the Unicode code point of the character.

The output of this program will be:


U+0041

This output represents the Unicode code point of the character `’A’`.

In summary, while Go doesn’t have a built-in `ord()` function like Python, we can use the `fmt.Printf()` function with the `%U` verb to achieve the same functionality.

Equivalent of Python ord in Go

In conclusion, the equivalent function to Python’s ord() in Go is the rune() function. Both functions are used to convert a character to its corresponding Unicode code point. However, there are some differences between the two functions. The ord() function returns an integer representing the Unicode code point of the given character, while the rune() function returns a rune, which is an alias for int32, representing the Unicode code point. Additionally, the ord() function can only accept a single character as input, while the rune() function can accept a string of characters as input. Overall, the rune() function is a useful tool for working with Unicode characters in Go, and can be used in a similar way to the ord() function in Python.

Contact Us