Home >Backend Development >Golang >Can Multiple Characters Be Represented in Rune Literals in Go?

Can Multiple Characters Be Represented in Rune Literals in Go?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-15 12:04:02457browse

Can Multiple Characters Be Represented in Rune Literals in Go?

Multiple Characters in Rune Literals in Go

In Go, rune literals are represented using single quotes (' '), similar to character literals in other programming languages.

Consider the following code snippet:

package main

import "fmt"

func main() {
    var a int

    fmt.Printf("Enter the number : ")
    fmt.Scanf('%d', &a)

    if a%2 == 0 {
        fmt.Println("%d Is even number", a)
    } else {
        fmt.Println("%d is odd number", a)
    }
}

When this code is run, you may encounter an error because of an incorrect format specifier. To represent a character literal in a format specifier, you need to enclose it in double quotes ("). The corrected code snippet should be:

fmt.Println("%d is odd number", a)

Rune Literals vs. String Literals

It's important to note that single quotes are specifically used for rune literals, while double quotes are used for string literals. In Go, strings are sequences of runes.

Single-Character Rune Literals:

  • 'a'
  • 'ä'
  • '本'
  • 't'
  • '

The above is the detailed content of Can Multiple Characters Be Represented in Rune Literals in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn