golang rune to Chinese

WBOY
Release: 2023-05-10 21:20:06
Original
674 people have browsed it

In the golang programming language, the character type rune is an integer type used to represent Unicode characters. When writing programs, we often need to convert rune type integer values ​​into corresponding Chinese characters.

The following introduces several methods of converting rune type to Chinese characters in golang.

  1. Directly convert rune type to string type

In golang, Chinese characters can be output by converting rune type to string type. The following is a sample code:

package main

import "fmt"

func main() {
    ch := '中'
    str := string(ch)
    fmt.Printf("%v
", str)
}
Copy after login

Running the above code will output: 中

In the above code, we assign the rune type character '中' to the variable ch and convert it to The string type in the variable str, and finally the value of the variable str is output through the fmt.Printf() function.

  1. Use the fmt.Printf() function to directly output rune type Chinese characters

In golang’s fmt package, there is a %q placeholder for output characters type of value. You can use this placeholder to output Chinese characters of rune type. The following is a sample code:

package main

import "fmt"

func main() {
    ch := '中'
    fmt.Printf("%q
", ch)
}
Copy after login

Running the above code will output: '中'

In the above code, we use the fmt.Printf() function to format the variable ch into the %q type , output the rune type Chinese characters in the variable ch.

  1. Use strconv package to convert rune type to string type

Golang’s strconv package contains many functions for type conversion, including converting rune type to String type functions. The following is a sample code:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    ch := '中'
    str := strconv.QuoteRune(ch)
    fmt.Printf("%v
", str)
}
Copy after login

Running the above code will output: "中"

In the above code, we use the strconv.QuoteRune() function to convert the rune type Chinese characters in the variable ch Convert to string type and output the value of variable str through the fmt.Printf() function.

  1. Use the unicode package to convert the rune type to Chinese characters

Golang’s unicode package contains many functions for Unicode character operations, including converting the rune type to Functions for Chinese characters. The following is a sample code:

package main

import (
    "fmt"
    "unicode/utf8"
)

func main() {
    ch := '中'
    buf := make([]byte, 3)
    n := utf8.EncodeRune(buf, ch)
    str := string(buf[:n])
    fmt.Printf("%v
", str)
}
Copy after login

Running the above code will output: Medium

In the above code, we use the EncodeRune() function in the unicode/utf8 package to convert the rune type in the variable ch Chinese characters are converted to byte slice type, and the string() function is used to convert the byte slice type to string type, and the value of the variable str is output through the fmt.Printf() function.

Summary

The above are several methods of converting rune type to Chinese characters in golang. Each of these methods has advantages and disadvantages and can be chosen according to specific needs. It should be noted that when converting, you need to ensure that the encoding method of the rune type is the same as the encoding method of the target string, otherwise garbled characters or exceptions may occur.

The above is the detailed content of golang rune to Chinese. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template