Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
750 views
in Technique[技术] by (71.8m points)

go - 奇怪的golang不匹配类型符文和字符串错误消息(strange golang mismatched types rune and string error message)

I am new to GO programming and I am having a problem with my program which uses a "switch" statement to process the value retrieved from a "map".

(我是GO编程的新手,我的程序存在问题,该程序使用“ switch”语句来处理从“ map”中检索的值。)

The map is declared as follows : var fields_map map[string]string

(映射声明如下:varfields_map map [string] string)

A value is later retrieved as follows : f_code, ok := fields_map["function"]

(稍后按如下方式检索值:f_code,确定:= fields_map [“ function”])

If "ok" has a true value I then do a "switch" on the retrieved code as follows

(如果“ ok”为真值,则对检索到的代码执行“ switch”,如下所示)

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

My problem is that for each of the "case" statements I get an error as follows : invalid case '' in switch on f_code (mismatched types rune and string)

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

According to one webpage I found "rune" is defined as follows: The Go language defines the word rune as an alias for the type int32

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

Why am I getting this error ?

(为什么会出现此错误?)

And why the reference to "rune" ?

(以及为什么提到“符文”?)

Both the key and value for my map are declared as "string" , so I am confused.

(我的地图的键和值都声明为“ string”,所以我很困惑。)

Any ideas ?

(有任何想法吗 ?)

I have created a reduced version of my code which has the same compile errors

(我已经创建了精简版代码,具有相同的编译错误)

     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
  ask by Barry Kimelman translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This error ( invalid case '' ) was seen here , and means you have not used actual double-quotes for your case values:

(在这里看到此错误( invalid case '' ), 意味着您没有使用实际的双引号作为大小写值:)

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

If you replace them by " , it should work, considering your map (and f_code ) are using string.

(如果将它们替换为" ,则应该起作用,因为您的地图(和f_code )正在使用字符串。)

The OP's example is in this playground , and does indeed generate the illegal rune literal error.

(OP的示例在这个操场上 ,的确会产生illegal rune literal错误。)

Using double-quotes, as in this playground , would yield no error.

(在操场上使用双引号不会产生任何错误。)

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>")
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...