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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…