Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在golang中存储unicode字符_String_Go_Unicode - Fatal编程技术网

String 在golang中存储unicode字符

String 在golang中存储unicode字符,string,go,unicode,String,Go,Unicode,我正在创建一个数据结构,用于存储单个unicode字符,然后进行比较 两个问题: 我使用什么数据类型 类型ds结构{ char//char应该是什么,这样我才能安全地比较两个ds? } 我需要一种方法来比较任意两个unicode字符串的第一个字符。有没有一个简单的方法可以做到这一点?基本上,如何检索字符串的第一个unicode字符 像这样:输入Char符文 注意比较,使用Unicode是一件复杂的事情。代码点符文在数值上很容易比较,U+0020==U+0020;U+1234

我正在创建一个数据结构,用于存储单个unicode字符,然后进行比较

两个问题:

我使用什么数据类型

类型ds结构{ char//char应该是什么,这样我才能安全地比较两个ds? }

我需要一种方法来比较任意两个unicode字符串的第一个字符。有没有一个简单的方法可以做到这一点?基本上,如何检索字符串的第一个unicode字符

像这样:输入Char符文

注意比较,使用Unicode是一件复杂的事情。代码点符文在数值上很容易比较,U+0020==U+0020;U+1234 要比较utf8字符串,需要检查它们的runevalue。Runevalue是utf8字符的int32值。使用标准软件包unicode/utf8。传递字符串[0:]以获取第一个字符

    test := "春节"
    runeValue, width := utf8.DecodeRuneInString(test[0:])
    fmt.Println(runeValue,width)
    fmt.Printf("%#U %d", runeValue, runeValue)
现在可以使用==运算符比较两个字符串的第一个字符的runeValue

如果要存储整个字符,还需要在字符串中存储字符串

type ds struct {
    char string // What should Char be so that I can safely compare two ds?
}
完整的代码演示了这一点:

package main

import (
    "fmt"
    "unicode/utf8"
)

type ds struct {
    char string // What should Char be so that I can safely compare two ds?
}

func main() {
    fmt.Println("Hello, playground")

    ds1 := ds{"春节"}
    ds2 := ds{"春节"}

    runeValue1, _ := utf8.DecodeRuneInString(ds1.char[0:])
    runeValue2, _ := utf8.DecodeRuneInString(ds2.char[0:])

    fmt.Printf("%#U %#U", runeValue1, runeValue2)

    if runeValue1 == runeValue2 {
        fmt.Println("\nFirst Char Same")
    } else {
        fmt.Println("\nDifferent")
    }
}

回答,我们可以用符文来比较

类型字符符文 要获取第一个unicode字符,只需执行[]runestr[0]
然后如何访问unicode字符串的第一个符文?unicode/utf8.DecodeRune[InString]:和DecodeRuneInstring现在我已经给出了演示这一点的完整代码。您不需要输入字符符文。除非您正在执行[]Charstr[0]