标签: rune

在Go中是否有与wcwidth()函数相同的功能?

POSIX函数wcwidth()计算在wchar_t终端上打印时给定的宽度.例如,wcwidth(L'A')返回1,wcwidth(L'?')返回2等.还有一个函数wcswidth()可以计算整个字符串的宽度 - 如果存在组合重音,这将非常有用.

Go标准库或补充库中是否存在类似的功能?如果没有,是否有一种简单的方法可以使某些东西足够相似?

unicode terminal go rune

2
推荐指数
1
解决办法
405
查看次数

不能使用unicode字符作为符文

似乎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)

有没有办法绕过这个,或者我应该忍受这个限制并使用其他东西?

unicode go rune

2
推荐指数
1
解决办法
1104
查看次数

如何区分类型切换中的符文和int32值?

拥有以下代码

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?

int32 go rune type-switch

1
推荐指数
1
解决办法
104
查看次数

在 go 中创建子字符串会创建一种新的符号

我正在比较字符串,并且有以下内容:

在此处输入图片说明

请注意,NEW 前面的“是不同的。

现在,当像这样调用我的函数时:

my_func(a[18:], b[18:])
Run Code Online (Sandbox Code Playgroud)

结果字符串令人惊讶:

在此处输入图片说明

我该怎么做才能剪掉这个奇怪的符号,为什么它会这样?

string utf-8 go rune

1
推荐指数
1
解决办法
41
查看次数

string(int), string(int32) and string([]int32) are all valid but string([]int) is invalid - what's the rationale here?

(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)?

string go slice rune

0
推荐指数
1
解决办法
159
查看次数

标签 统计

go ×5

rune ×5

string ×2

unicode ×2

int32 ×1

slice ×1

terminal ×1

type-switch ×1

utf-8 ×1