【问题标题】:Strange error message: mismatched types rune and string奇怪的错误信息:rune 和 string 类型不匹配
【发布时间】:2019-12-01 00:11:12
【问题描述】:

我是 Go 编程新手,我的程序使用“switch”语句处理从“map”检索的值时遇到问题。

地图声明如下:

var fields_map map[string]string

稍后按如下方式检索一个值:

f_code, ok := fields_map["function"]

如果“ok”有一个真值,我会在检索到的代码上做一个“切换”,如下所示

    switch f_code {
        case "meta":
        case "users":
        case "history":
        default:
    }

我的问题是,对于每个“case”语句,我都会收到如下错误: f_code 开关中的无效大小写“\u0000”(符文和字符串类型不匹配)

根据我发现的一个网页,“符文”定义如下: Go 语言将 rune 定义为 int32 类型的别名

为什么会出现这个错误?为什么提到“符文”? 我的地图的键和值都被声明为 "string" ,所以我很困惑。

有什么想法吗?

我创建了具有相同编译错误的代码的简化版本

     1  package main
     2  
     3  import (
     4      "fmt"
     5  )
     6  
     7  var fields_count int
     8  var fields_map map[string]string
     9  
    10  func parse_fields() {
    11      fields_count = 0
    12      fields_map = make(map[string]string)  // initialize the map
    13  
    14  } // parse_fields
    15  
    16  func main() {
    17  
    18      parse_fields()
    19      f_code, ok := fields_map["function"] // test for existance of function code
    20      if ok {
    21          switch f_code {
    22              case 'meta':
    23                  break;
    24              case 'users':
    25                  break;
    26              case 'history':
    27                  break;
    28              default:
    29                  break;
    30          }// switch
    31      } else {
    32          fmt.Println("<H3>No function code detected</H3>")
    33      }
    34  
    35  } // main

【问题讨论】:

  • 报错提示地图不是map[string]string,而是map[string]rune。
  • 我的声明没有提到“符文”。为什么 GO 编译器会抱怨 rune?
  • 尽量减少代码,然后在Go Playground 上发布整个减少的代码和/或链接。如果我们看不到实际来源,我们都只能猜测。
  • 您只发布了部分代码。基于此:要么声明不是您所说的;要么或 f_code 或 fields_map 正在遮蔽另一个变量;或者您在此处粘贴的 switch-case 与您正在编译的不同。

标签: go


【解决方案1】:

这个错误 (invalid case '\u0000') 是 seen here,这意味着您没有使用实际的双引号作为您的案例值:

switch f_code {
    case 'meta':
    case 'users':
    case 'history':
    default:
}

如果您将它们替换为",它应该可以工作,考虑到您的地图(和f_code)正在使用字符串。

OP 的示例在this playground 中,确实会产生illegal rune literal 错误。

使用双引号,如this playground,不会产生错误。

parse_fields()
f_code, ok := fields_map["function"] // test for existance of function code
if ok {
    switch f_code {
    case "meta":
        break
    case "users":
        break
    case "history":
        break
    default:
        break
    } // switch
} else {
    fmt.Println("<H3>No function code detected</H3>")
}

【讨论】:

  • 用单引号代替双引号不会改变任何东西。我仍然收到相同的错误消息
  • @BarryKimelman 将单引号替换为双引号,而不是反之。
  • 我尝试过同时使用单引号和双引号。在这两种情况下,我都会收到相同的错误消息。
【解决方案2】:

反射包用于获取值的类型,也可以根据类型进行切换。

package main

import (
        "fmt"
        "reflect"
    )

func main() {
     myMap := map[string]string{ "test1":"testing", "test2":"testing2" }
     value, ok := myMap["test1"]
     fmt.Println(reflect.TypeOf(value), value)
     if (ok) {
           switch value {
               case "debug1":
                  fmt.Println("not what i meant")
               case "testing":
                  fmt.Println("This is what i am looking for")
               default:
                  fmt.Println("default value ")
           }
      }
}

输出:

字符串测试

这就是我要找的东西

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    相关资源
    最近更新 更多