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.

GoForm Validation

Code

snippet
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

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.