Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

无法解析数组参数 #16

Closed
caoyong2619 opened this issue Jan 10, 2017 · 10 comments
Closed

无法解析数组参数 #16

caoyong2619 opened this issue Jan 10, 2017 · 10 comments

Comments

@caoyong2619
Copy link

type ConsoleForm struct {
	Operation string   `form:"operation"`
	Command   string   `form:"command"`
	Args      []string `form:"args"`
}

发送参数operation=test&command=run&args[]=1&args[]=2

args无法映射到struct中

@unknwon
Copy link
Contributor

unknwon commented Jan 22, 2017

你这用法不对吧,operation=test&command=run&args=1&args=2 试试?

@caoyong2619
Copy link
Author

caoyong2619 commented Feb 4, 2017

写法没问题哦..http的数组是这样传的 PHP和python都能正确接收
追踪代码的时候*http.Request里也能找到1和2这两个值,但是映射不到struct里

@unknwon
Copy link
Contributor

unknwon commented Feb 5, 2017

追踪代码的时候*http.Request里也能找到1和2这两个值

代码举证?。。。

另外你试了我说的方法了吗。。?

@caoyong2619
Copy link
Author

caoyong2619 commented Feb 6, 2017

operation=test&command=run&args=1&args=2
这段试过了,也无法映射到strcut里
但是如果改为

type ConsoleForm struct {
	Operation string   `form:"operation"`
	Command   string   `form:"command"`
	Args      string `form:"args"`
}

可以得到form.Args为2
这并非http中数组的传递,第二个args会覆盖第一个

在php中operation=test&command=run&args[]=1&args[]=2
GET请求中能得到

$_GET['args']的值为 array(1, 2)

operation=test&command=run&args=1&args=2

在php中只能得到$_GET['args']为 2

原因同上

@unknwon
Copy link
Contributor

unknwon commented Feb 6, 2017

我问的是 Go 的代码举证。。

@spkinger
Copy link

spkinger commented Nov 8, 2018

type ConsoleForm struct {
	Args      []string form:"args[]";
}

我也遇到这个问题,我按如上方式绑定,
POST参数args[]
args[0]=1
args[1]=2

Args依然为空数组[]

@unknwon
Copy link
Contributor

unknwon commented Nov 8, 2018

不要使用PHP的方式

试试 command=run&args=1&args=2

@unknwon unknwon closed this as completed Nov 8, 2018
@spkinger
Copy link

spkinger commented Nov 8, 2018

https://github.com/gin-gonic/gin/issues/129
这个是隔壁gin的绑定方式

如果像下面这样的参数

<input type="checkbox" name="args[]" value="1" />
<input type="checkbox" name="args[]" value="2" />

macaron应该怎样接收呢

@unknwon
Copy link
Contributor

unknwon commented Nov 8, 2018

我测了完全没问题啊,用的 gin-gonic/gin#129 (comment) 里 form 的例子:

package main

import (
	"fmt"

	"github.com/go-macaron/binding"
	"gopkg.in/macaron.v1"
)

type ConsoleForm struct {
	Args []string `form:"colors[]"`
}

func main() {
	m := macaron.Classic()
	m.Use(macaron.Renderer())
	m.Post("/", binding.Bind(ConsoleForm{}), func(form ConsoleForm) {
		fmt.Println(form)
	})
	m.Run()
}
[Macaron] 2018-11-08 04:00:38: Started POST / for [::1]
{[red green blue]}

@helloteemo
Copy link

type ConsoleForm struct {
	Operation string   `form:"operation"`
	Command   string   `form:"command"`
	Args      []string `form:"args[]"`
}

可以使用这样来获取args[]参数绑定,但是这样做就以为着args参数不能被绑定到

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants