POSIX函数wcwidth()计算在wchar_t终端上打印时给定的宽度.例如,wcwidth(L'A')返回1,wcwidth(L'?')返回2等.还有一个函数wcswidth()可以计算整个字符串的宽度 - 如果存在组合重音,这将非常有用.
Go标准库或补充库中是否存在类似的功能?如果没有,是否有一种简单的方法可以使某些东西足够相似?
似乎golang不支持其符文的所有unicode字符
package main
import "fmt"
func main() {
standardSuits := []rune{'??', '??', '??', '??'}
fmt.Println(standardSuits)
}
Run Code Online (Sandbox Code Playgroud)
生成以下错误:
./main.go:6: missing '
./main.go:6: invalid identifier character U+FE0F '?'
./main.go:6: syntax error: unexpected ?, expecting comma or }
./main.go:6: missing '
./main.go:6: invalid identifier character U+FE0F '?'
./main.go:6: missing '
./main.go:6: invalid identifier character U+FE0F '?'
./main.go:6: missing '
./main.go:6: invalid identifier character U+FE0F '?'
./main.go:6: missing '
./main.go:6: too many errors
Run Code Online (Sandbox Code Playgroud)
有没有办法绕过这个,或者我应该忍受这个限制并使用其他东西?
拥有以下代码
var v interface{}
v = rune(1)
switch v.(type) {
case int32:
fmt.Println("int32")
case rune:
fmt.Println("rune")
}
Run Code Online (Sandbox Code Playgroud)
我收到编译错误
tmp/sandbox193184648/main.go:14: duplicate case rune in type switch
previous case at tmp/sandbox193184648/main.go:12
Run Code Online (Sandbox Code Playgroud)
如果我用我自己的类型换行符文,那么type-switch会编译并运行
type myrune rune
var v interface{}
v = myrune(1)
switch v.(type) {
case int32:
fmt.Println("int32")
case myrune:
fmt.Println("rune")
}
Run Code Online (Sandbox Code Playgroud)
请参阅https://play.golang.org/p/2lMRlpCLzX
这是为什么?如何在类型切换中区分符文和int32?
我正在比较字符串,并且有以下内容:
请注意,NEW 前面的“是不同的。
现在,当像这样调用我的函数时:
my_func(a[18:], b[18:])
Run Code Online (Sandbox Code Playgroud)
结果字符串令人惊讶:
我该怎么做才能剪掉这个奇怪的符号,为什么它会这样?
(I'm using Go 1.14.6.)
The following statements would all output the char a
Println(string(int(97) ) )
Println(string(int32(97) ) )
Println(string([]int32{97} ) )
Run Code Online (Sandbox Code Playgroud)
But
Println(string([]int{97} ) )
Run Code Online (Sandbox Code Playgroud)
would cause compile error
cannot convert []int literal (type []int) to type string
Run Code Online (Sandbox Code Playgroud)
The behavior is confusing to me. If it handles string(int) the same as string(int32), why it handles string([]int) different from string([]int32)?