dongxianghui3709 2019-02-09 14:04
浏览 84
已采纳

如何将int32 Unicode转换为字符串

I have a function that receives a list of hostnames , line by line, in an int32 slice format. This is the function:

func HandlePipeList(targetsList []int32) {
    //Print output item by item
    for i := 0; i < len(targetsList); i++ {
        fmt.Printf("%c", targetsList[i])
    }
}

Since I'm converting it to %c with fmt, it works and prints the hostnames correctly. My problem arises when I try to pass targetsList as a string to another function. How can I make the same convertion for targetsList so that the lines could be passed as string? (strconv.Itoa didn't work here).

  • 写回答

2条回答 默认 最新

  • dougong5817 2019-02-09 14:14
    关注

    A unicode code point in Go is a rune. Go type rune is an alias for Go type int32.


    The Go Programming Language Specification

    Numeric types

    int32   the set of all signed 32-bit integers
    
    rune    alias for int32
    

    Conversions

    Conversions to and from a string type

    Converting a slice of runes to a string type yields a string that is the concatenation of the individual rune values converted to strings.


    Use a string type conversion. For example,

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        targetsList := []int32("Testing, testing, ...")
    
        str := string(targetsList)
        fmt.Println(str)
    }
    

    Playground: https://play.golang.org/p/d-s0uLFl9MG

    Output:

    Testing, testing, ...
    

    Reference: The Go Blog: Strings, bytes, runes and characters in Go

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测