ParseForm Reads Submitted Form Fields
ParseForm decodes an application/x-www-form-urlencoded body so repeated keys become string slices and Form.Get returns the first submitted value.
Code
package main
import (
"fmt"
"net/http"
"strings"
)
func main() {
// 构造一个带表单体和 Content-Type 的请求,模拟浏览器提交
req, _ := http.NewRequest("POST", "/signup",
strings.NewReader("[email protected]&tag=go&tag=http"))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// ParseForm 解析表单体:同名键聚合进 []string
if err := req.ParseForm(); err != nil {
fmt.Println("bad form:", err)
return
}
// Form.Get 取第一个值;Form["tag"] 拿到全部重复值
fmt.Println("email:", req.Form.Get("email"))
fmt.Println("tags:", req.Form["tag"])
}Reading the code
The snippet is organized around the function main.
Error handling is inline: 1 place test err != nil and branch immediately.
Watch out for
- Zero third-party dependencies — the runtime supplies every API used here.
Putting it to use
Entry signature: func main()
The output arrives labeled — bad form:, email:, tags: — when the file runs.
Core lines
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.
FAQ
- What does ParseForm Reads Submitted Form Fields print when run?
- Invoke the file as-is — the `bad form:`, `email:`, `tags:` prefixes identify which step printed each line.
Report a problem with this exampleOpens a pre-filled issue on GitHub.