英文:
Golang set a maps key as a variable for its value
问题
我需要遍历 HTML 表单的输入值,并将它们放入 MySQL 数据库中。我目前正在使用 r.Form
来获取一个映射(map)。这样我就不必为每个值使用 r.Form.Get("date")
,虽然这种方法有效,但当我尝试将这些值放入数据库时,编译没有问题,但在浏览器中点击提交后,我得到了 sql: converting argument #0's type: unsupported type []string, a slice
的错误。我可以通过以下方式解决这个问题:
`date := strings.Join(m["date"], "")`
但是对于30多个值来说,这样做太麻烦了,特别是因为一些提交的值将从先前的数据库条目中使用 HTML 模板创建。如果以后需要更改或添加更多值,似乎应该有一种更高效的方法。我看到过使用 for key, val := range m {}
的方法,但不幸的是,我只做了一周左右,我无法弄清楚如何在每次迭代后保留值并更改它们被设置为的变量。所以在这之后:
for key, val := range m {
x := m[key]
}
这样它将输出相应的等价形式:
keyname := keyvalue
每次更改 keyname,使其与映射中的键名相同,例如:
date := 2015-8-13
time := 18:56:11
或者如果有更简单的方法来解决这个错误,那就是为每个值创建一个变量。
英文:
I need to go through an html forms input values and place them into an mysql datbase. Which I'm currently using r.Form
to get a map. So that i don't have to use r.Form.Get("date")
for each which works but when i try to put the values into a database. It compiles just fine but i get sql: converting argument #0's type: unsupported type []string, a slice
after i click submit in the browser. I can get around this by doing
`date := strings.Join(m["date"], "")`
but doing that for 30+ values especially since some of the submited values will be created from previous database entries using html templates. If i have to change or add more later seems like there must be a more efficient way I've seen for key, val := range m {}
but unfortunately I've only been doing this for about a week and i can't figure out how to keep the values and change the variable they get set to after each iteration. So that after
for key, val := range m {
x := m[key]
}
so that it will put out the equivalent
keyname := keyvalue
changing the keyname each time to be the same as the keyname in the map ie
date := 2015-8-13
time := 18:56:11
or if there's an easier way around this error then to create a varible for each one.
答案1
得分: 2
一个HTML表单可以有多个值对应一个键。这就是为什么请求表单字段被定义为字符串切片的映射。请求表单被声明为
Form url.Values
而url.Values被声明为
type Values map[string][]string
你可以通过以下方式访问一个键的第一个值:
var value string
if values := req.Form[key]; len(values) > 0 {
value = values[0]
}
url.Values Get辅助方法简化了这段代码:
value := req.Form.Get(key)
http.Request FormValue辅助方法进一步简化了代码:
value := req.FormValue(key)
你可以通过以下方式遍历键和值:
for key, values := range req.Form {
for _, value := range values {
fmt.Println(key, value)
}
}
如果你想遍历一个键的第一个值,可以使用以下代码:
for key, values := range req.Form {
if len(values) > 0 {
value := values[0]
fmt.Println(key, value)
}
}
在访问req.Form之前,调用req.ParseForm来解析查询字符串和请求体。
英文:
An HTML form can have multiple values for a single key. This is why the request form field is defined to be a map of string slices. The request Form is declared as
Form url.Values
and url.Values is declared as
type Values map[string][]string
You can access the first value for a key using:
var value string
if values := req.Form[key]; len(values) > 0 {
value = values[0]
}
The url.Values Get helper method simplifies this code to:
value := req.Form.Get(key)
The http.Request FormValue helper method simplifies it a bit more:
value := req.FormValue(key)
You iterate through keys and values using:
for key, values := range req.Form {
for _, value := range values {
fmt.Println(key, value)
}
}
If you want to iterate over the first value for a key, then use this code:
for key, values := range req.Form {
if len(values) > 0 {
value := values[0]
fmt.Println(key, value)
}
}
Before accessing req.Form, call req.ParseForm to parse the query string and request body.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论