go - 无效操作 : (operator - not defined on string)

标签 go operator-keyword

arrayAll := []string{"a", "b", "c", "d", "e"}

x := p[arrayAll[i]-"a"]

go 不支持运算符“-”, 那么如何获取数组的索引:arrayAll[i]-"a"

最佳答案

如何在字符串上定义运算符 -?调用 "Hello"-"World" 后你期望得到什么结果?

您是否尝试对单个字符进行操作?这是明确定义的,您可能期望 'c'-'a' 确实等于 2。考虑:

arrayAll := []byte{'a', 'b', 'c'} (or simply "abc")
x := p[arrayAll[2] - 'a']

不管怎样,您很可能不想减去字符串,而是字符的整数表示。

关于go - 无效操作 : (operator - not defined on string),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29893723/

相关文章:

javascript - 内部网络上的 Websockets 地址需要静态 ip?

C++ 跟踪原始类型值的变化

c++ - 运算符重载 C++ +=

go - 如何使用cgo静态链接go中的c库?

go - 将非接口(interface)称为 "*er"是惯用的吗

function - F# 中的 + 是什么?

c++ - 为什么virtual operator==是在Base类而不是Derived类上调用的?

c++ - 转换运算符在这种情况下有效吗?

go - UTC()调用是否在rand.Seed(time.Now()。UTC()。UnixNano())中冗余?

go - gorm rest API 中的模式搜索