beego提供了一套web开发的框架。但咱们在开发过程当中遇到了一些问题,现汇总以下。php
测试1:测试只有keys数组的状况web
func (this *TestController) Index() { keys := make([]string, 0) this.Ctx.Input.Bind(&keys, "keys") fmt.Println(keys, len(keys)) }
测试结果:api
url:http://api.query.qihoo.net:8090/test/index?keys[]=test&keys[]=test数组
结果:[] 0框架
反复测试屡次,结果相同测试
测试2:测试有keys数组还有vals数组的状况ui
func (this *TestController) Index() { keys := make([]string, 0) this.Ctx.Input.Bind(&keys, "keys") var vals map[]string vals = make(map[]string, 0) this.Ctx.Input.Bind(&vals, "vals") fmt.Println(keys, vals, len(keys), len(vals)) }
测试结果:this
url:http://api.query.qihoo.net:8090/test/index?keys[]=test&keys[]=test&vals[]=test&vals[]=test编码
[] [] 0 0url
屡次测试,结果相同
测试3:测试有keys数组,有test字符串
func (this *TestController) Index() { var test string this.Ctx.Input.Bind(&test, "test") fmt.Println(test) keys := make([]string, 0) this.Ctx.Input.Bind(&keys, "keys") fmt.Println(keys, len(keys)) }
测试url:http://api.query.qihoo.net:8090/test/index?keys[]=test&keys[]=test
输出:gotest:reflect.Set: value of type int is not assignable to type string
测试url:http://api.query.qihoo.net:8090/test/index?keys[]=test&keys[]=test&test=
输出:gotest:reflect.Set: value of type int is not assignable to type string
测试url:http://api.query.qihoo.net:8090/test/index?keys[]=test&keys[]=test&test=test
输出:
test
[test test] 2
测试4:有keys数组,有test字符串,用GetString来获取test
func (this *TestController) Index() { var test string test = this.GetString("test") fmt.Println(test) keys := make([]string, 0) this.Ctx.Input.Bind(&keys, "keys") fmt.Println(keys, len(keys)) }
测试url:http://api.query.qihoo.net:8090/test/index?keys[]=test&keys[]=test&test=test
结果:
test
[test test] 2
测试url:http://api.query.qihoo.net:8090/test/index?keys[]=test&keys[]=test
结果:
[test test] 2
进一步测试
测试4:
func (this *TestController) Index() { var test string test = this.GetString("test") fmt.Println(test) keys := make([]string, 0) this.Ctx.Input.Bind(&keys, "keys") fmt.Println(keys, len(keys)) }
接下来的测试不会设置test,输出结果也不包含test的输出
测试url:http://api.query.qihoo.net:8090/test/index?keys[]=test&keys[]=test
输出:[test test] 2
测试url:http://api.query.qihoo.net:8090/test/index?keys[]=test&keys[]=
输出:[test ] 2
测试url:http://api.query.qihoo.net:8090/test/index?keys[0]=test&keys[1]=test
输出:[test test] 2
测试url:http://api.query.qihoo.net:8090/test/index?keys[0]=test&keys[1]=
输出:gotest:reflect.Set: value of type int is not assignable to type string
测试url:http://api.query.qihoo.net:8090/test/index?keys[]=test&keys[]=test&keys[0]=test&keys[1]=test
输出:[test test test test] 4
测试url:http://api.query.qihoo.net:8090/test/index?keys[]=test&keys[]=test&keys[0]=test&keys[1]=test&keys[2]=
输出:gotest:reflect.Set: value of type int is not assignable to type string
测试url:http://api.query.qihoo.net:8090/test/index?keys[]=test&keys[]=&keys[0]=test&keys[1]=test&keys[2]=test
输出:[test test test test ] 5
测试结果显示:
使用beego框架,想要获取数组必须具有几个条件:
1,必须有非数组的变量存在(string能够,其它类型int-bool等的没作测试),若是没有单个变量只有数组(不管是单个数组或者多个数组)则全部的数组获均取不到元素;
2,若是url是keys[1]=test&keys[2]=test这样的,必定得保证每一个项都要有值,不能为空,也就是不能存在keys[1]=这样的项
注意事项:
非数组的单个变量若是不肯定的话,最好用GetString,GetString不论该变量是否在url中被设置都能正常工做。若是用Bind,必定要保证该变量是有值的,不然报错
容许这样的url:keys[]=test&keys[]=,不容许keys[2]这样的url:keys[0]=test&keys[1]=test&keys[2]=
实际开发中:
1,注意事项1很是有用,咱们能够用一行代码完成:var _ = this.GetString("test")
2,php的http_huild_query处理后的url是进行了urlencode编码,并且数组是带下标的(相似于keys[0]=1&keys[1]=2),从0开始编号。大部分工做正常,在值为空字符串是会有问题(keys[0]=test&keys[1]=test&keys[2]=),咱们如何处理这部分状况呢?以下实验给出告终论:用map[string]string而不是[]string
继续作实验。
实验5:
func (this *TestController) Index() { var _= this.GetString("test") var keys map[string]string keys = make(map[string]string, 0) this.Ctx.Input.Bind(&keys, "keys") fmt.Println(keys, len(keys)) }
测试url:http://api.query.qihoo.net:8090/test/index?keys[0]=test&keys[1]=test&keys[2]=test
结果:map[0:test 1:test 2:test] 3
测试url:http://api.query.qihoo.net:8090/test/index?keys[0]=test&keys[1]=test&keys[2]=
结果:map[0:test 1:test 2:] 3
测试url:http://api.query.qihoo.net:8090/test/index?keys[0]=test&keys[1]=test&keys[2]=test&keys[]=100
结果:map[0:test 1:test 2:test :100] 4
咱们看到用map[string]string来代替[]string可以解决报错的问题,同时也有局限:再也不兼容keys[]这样的数组元素。
因此区分两种不兼容的使用场景,只能取其中一种用:1,数组元素所有用keys[],而且不容许任意一个keys[]有空值,可用[]string的Bind;2,数组元素所有用keys[0],keys[1],keys[2],容许有空值,map[string]string的Bind,若是要用数组而不是map,须要手工转化。