top

Package gorilla/schema fills a struct with form values.

The basic usage is really simple. Given this struct:

type Person struct {
	Name  string
	Phone string
}

...we can fill it passing a map to the Decode() function:

values := map[string][]string{
	"Name":  {"John"},
	"Phone": {"999-999-999"},
}
person := new(Person)
decoder := schema.NewDecoder()
decoder.Decode(person, values)

This is just a simple example and it doesn't make a lot of sense to create the map manually. Typically it will come from a http.Request object and will be of type url.Values: http.Request.Form or http.Request.MultipartForm:

func MyHandler(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm()

	if err != nil {
		// Handle error
	}

	decoder := schema.NewDecoder()
	// r.PostForm is a map of our POST form values
	err := decoder.Decode(person, r.PostForm)

	if err != nil {
		// Handle error
	}

	// Do something with person.Name or person.Phone
}

Note: it is a good idea to set a Decoder instance as a package global, because it caches meta-data about structs, and a instance can be shared safely:

var decoder = schema.NewDecoder()

To define custom names for fields, use a struct tag "schema". To not populate certain fields, use a dash for the name and it will be ignored:

type Person struct {
	Name  string `schema:"name"`  // custom name
	Phone string `schema:"phone"` // custom name
	Admin bool   `schema:"-"`     // this field is never set
}

The supported field types in the destination struct are:

* bool
* float variants (float32, float64)
* int variants (int, int8, int16, int32, int64)
* string
* uint variants (uint, uint8, uint16, uint32, uint64)
* struct
* a pointer to one of the above types
* a slice or a pointer to a slice of one of the above types

Non-supported types are simply ignored, however custom types can be registered to be converted.

To fill nested structs, keys must use a dotted notation as the "path" for the field. So for example, to fill the struct Person below:

type Phone struct {
	Label  string
	Number string
}

type Person struct {
	Name  string
	Phone Phone
}

...the source map must have the keys "Name", "Phone.Label" and "Phone.Number". This means that an HTML form to fill a Person struct must look like this:

<form>
	<input type="text" name="Name">
	<input type="text" name="Phone.Label">
	<input type="text" name="Phone.Number">
</form>

Single values are filled using the first value for a key from the source map. Slices are filled using all values for a key from the source map. So to fill a Person with multiple Phone values, like:

type Person struct {
	Name   string
	Phones []Phone
}

...an HTML form that accepts three Phone values would look like this:

<form>
	<input type="text" name="Name">
	<input type="text" name="Phones.0.Label">
	<input type="text" name="Phones.0.Number">
	<input type="text" name="Phones.1.Label">
	<input type="text" name="Phones.1.Number">
	<input type="text" name="Phones.2.Label">
	<input type="text" name="Phones.2.Number">
</form>

Notice that only for slices of structs the slice index is required. This is needed for disambiguation: if the nested struct also had a slice field, we could not translate multiple values to it if we did not use an index for the parent struct.

There's also the possibility to create a custom type that implements the TextUnmarshaler interface, and in this case there's no need to registry a converter, like:

type Person struct {
  Emails []Email
}

type Email struct {
  *mail.Address
}

func (e *Email) UnmarshalText(text []byte) (err error) {
	e.Address, err = mail.ParseAddress(string(text))
	return
}

...an HTML form that accepts three Email values would look like this:

<form>
	<input type="email" name="Emails.0">
	<input type="email" name="Emails.1">
	<input type="email" name="Emails.2">
</form>

Imported by 388 package(s)

  1. bitbucket.org/kirkmcdonald/broken
  2. bitbucket.org/kirkmcdonald/handling/forms
  3. github.com/42wim/matterbridge/matterhook
  4. github.com/9elements/gotoolbox/controllers
  5. github.com/AlexKomrakov/gohub/server
  6. github.com/AlexSnet/go-restful/examples
  7. github.com/CptSpaceToaster/adventurebot
  8. github.com/CptSpaceToaster/slackbot
  9. github.com/Crockeo/cerekhillen/controllers
  10. github.com/Crockeo/cerekhillen/middleware
  11. github.com/DrewWeth/eta/src/app/controllers/commentcontroller
  12. github.com/DrewWeth/eta/src/app/controllers/postcontroller
  13. github.com/ElectronicsExtreme/exeoauth2/handler/change_password
  14. github.com/ElectronicsExtreme/exeoauth2/handler/user
  15. github.com/EverythingMe/vertex
  16. github.com/FSX/WAD/beep
  17. github.com/FurqanSoftware/papyrus/ui
  18. github.com/Ishukone/codetainer
  19. github.com/Ivan8R/goShop
  20. github.com/LastZactionHero/graynote_server
  21. github.com/Leon2012/go-restful/examples
  22. github.com/Logiraptor/mosaic
  23. github.com/LunaNode/lobster
  24. github.com/LunaNode/lobster/core/support
  25. github.com/Manbeardo/eventreporter
  26. github.com/Manbeardo/go-restful/examples
  27. github.com/Misrab/bookshare-backend-api/handlers
  28. github.com/Misrab/goapi/handlers
  29. github.com/Moddus/go-restful/examples
  30. github.com/MrToy/business
  31. github.com/NYC-Camp/authenticator/libuser
  32. github.com/Orion90/guessify
  33. github.com/OsvaldoTCF/o2go/controllers
  34. github.com/OsvaldoTCF/order2go/controllers
  35. github.com/PepperSalt42/api
  36. github.com/ReSc/godel/core/mvc
  37. github.com/RobotsAndPencils/marvin
  38. github.com/StefanKjartansson/stadfangaskra-rest/rest
  39. github.com/SvenDowideit/codetainer
  40. github.com/Throckmortra/go-restful/examples
  41. github.com/TimothyJones/trec-liveqa-server
  42. github.com/YouthBuild-USA/godata/forms
  43. github.com/Zenithar/api/utils
  44. github.com/aarondl/cinotify/coveralls
  45. github.com/abci/go-restful/examples
  46. github.com/abh/dellingr
  47. github.com/abhiunc/go-restful/examples
  48. github.com/agamble/lockbox
  49. github.com/aiyi/go-restful/examples
  50. github.com/alaska/go-restful/examples
  51. github.com/alecthomas/rapid
  52. github.com/alesstimec/juju/apiserver
  53. github.com/alexkomrakov/gohub/server
  54. github.com/alexsaveliev/vcsstore/server
  55. github.com/alt234/affadmin-go/controllers
  56. github.com/alt234/affadmin/controllers
  57. github.com/aluzzardi/go-restful/examples
  58. github.com/aquilax/novigeroi-go
  59. github.com/arjandepooter/slack-exact
  60. github.com/athega/whistler
  61. github.com/augustoroman/handler
  62. github.com/axw/juju/apiserver
  63. github.com/barnslig/oauth-account-manager
  64. github.com/bbuck/graphql-go-handler
  65. github.com/bcampbell/superfastmatch/document
  66. github.com/bcampbell/superfastmatch/posting
  67. github.com/beati/mortg-exp/mortg-exp-gameserver
  68. github.com/bkputnam/gamestatemanager
  69. github.com/bmbernie/utron/fixtures/todo/controllers
  70. github.com/bosssauce/ponzu/system/admin
  71. github.com/bosssauce/ponzu/system/db
  72. github.com/brendandburns/go-restful/examples
  73. github.com/bulletind/khabar-admin/handlers/preferences
  74. github.com/bussiere/LDVLH
  75. github.com/c0gent/unframed
  76. github.com/caesarxuchao/go-restful/examples
  77. github.com/caikaijie/igo-middleware/rest
  78. github.com/carbocation/go.graf
  79. github.com/carbocation/go.graf/example
  80. github.com/cbroglie/go-restful/examples
  81. github.com/cedmundo/resty
  82. github.com/celeskyking/codetainer
  83. github.com/changboter/go-restful/examples
  84. github.com/chaosknight/aha/json_service
  85. github.com/chrisscott/flippy
  86. github.com/clsung/go-restful/examples
  87. github.com/cocoonlife/gop
  88. github.com/codetainerapp/codetainer
  89. github.com/cptspacetoaster/adventurebot
  90. github.com/crockeo/cerekhillen/controllers
  91. github.com/crockeo/cerekhillen/middleware
  92. github.com/cwc/asset-tracker
  93. github.com/cwc/webconf
  94. github.com/davidbogue/lucid/controllers
  95. github.com/dbtleonia/bowl
  96. github.com/ddysher/go-restful/examples
  97. github.com/deet/govenmo/local_sandbox
  98. github.com/derekdowling/bursa/controller/home
  99. github.com/devchakraborty/stokr/server
  100. github.com/diegogub/framgo
  101. github.com/dimitern/juju/apiserver
  102. github.com/dominicphillips/slackbot
  103. github.com/donovanhide/superfastmatch/document
  104. github.com/donovanhide/superfastmatch/posting
  105. github.com/donovanhide/superfastmatch/query
  106. github.com/dooferlad/juju/apiserver
  107. github.com/drewweth/eta/src/app/controllers/commentcontroller
  108. github.com/drewweth/eta/src/app/controllers/postcontroller
  109. github.com/duane/go-restful/examples
  110. github.com/duanebester/go-restful/examples
  111. github.com/edoardo849/go-restful/examples
  112. github.com/elivoa/got/core/lifecircle
  113. github.com/elivoa/got/coreservice/coercion
  114. github.com/emicklei/go-restful/examples
  115. github.com/ericsnowcurrently/juju/apiserver
  116. github.com/ernestoalejo/closurer/app
  117. github.com/ernestokarim/closurer/app
  118. github.com/ernestokarim/serverlib/app
  119. github.com/evq/go-restful/examples
  120. github.com/eyesight-tech/go-restful/examples
  121. github.com/fadib/thesrc/api
  122. github.com/fadib/thesrc/app
  123. github.com/fallenhitokiri/leeroyci/web
  124. github.com/faxal/idp/providers/form
  125. github.com/fitstar/slackbot
  126. github.com/fmpwizard/go-restful/examples
  127. github.com/foxycoder/goqu
  128. github.com/franciscocpg/go-restful/examples
  129. github.com/frankban/juju/apiserver
  130. github.com/fromkeith/gorest
  131. github.com/gaego/auth/dev
  132. github.com/gaego/auth/password
  133. github.com/gaigepr/list-app/handlers
  134. github.com/gdamore/go-restful/examples
  135. github.com/gedn/gowiki
  136. github.com/genghisjahn/slackbot
  137. github.com/gerbenjacobs/go-restful/examples
  138. github.com/gernest/utron/fixtures/todo/controllers
  139. github.com/gernest/zedlist/routes/dash
  140. github.com/ggitau/google-go
  141. github.com/gistia/slackbot
  142. github.com/gnotclub/albumify-next/controllers
  143. github.com/go-authboss/authboss-sample
  144. github.com/go-web/httpio
  145. github.com/goanywhere/rex/form
  146. github.com/gocarina/pi
  147. github.com/gopheracademy/slackbot
  148. github.com/gophergala/gotoolbox/controllers
  149. github.com/gophergala2016/papyrus/ui
  150. github.com/guilhermebr/slackbot
  151. github.com/gust1n/thesrc/api
  152. github.com/gust1n/thesrc/app
  153. github.com/guus-vanweelden/zedlist/routes/dash
  154. github.com/hackclub/orbit/api
  155. github.com/hackedu/orbit/api
  156. github.com/halk/in-common/api
  157. github.com/hariharan-uno/athena
  158. github.com/hariharan-uno/webhasher
  159. github.com/hashtock/hashtock-go/webapp
  160. github.com/hc5duke/bones/web/forms
  161. github.com/henrylee2cn/go-restful/examples
  162. github.com/hjr265/loadcat/ui
  163. github.com/hoenirvili/juju/apiserver
  164. github.com/housinganywhere/handler
  165. github.com/howbazaar/juju/apiserver
  166. github.com/huichen/aha/json_service
  167. github.com/ian-kent/go-webapp/render
  168. github.com/ian-kent/service.go/web/render
  169. github.com/idebruijn/go-restful/examples
  170. github.com/ilgooz/formutils
  171. github.com/itshosted/mcore/valid/valid
  172. github.com/jabong/go-restful/examples
  173. github.com/jandre/codetainer
  174. github.com/jasonrdsouza/foosball_monkey
  175. github.com/javinc/playgo/goryo/controllers
  176. github.com/javinc/puto
  177. github.com/jbaikge/ingress-inventory
  178. github.com/jbert/gop
  179. github.com/jcharlesworth/thesrc/api
  180. github.com/jcharlesworth/thesrc/app
  181. github.com/jeeyoungk/reversi/web
  182. github.com/jefferai/go-restful/examples
  183. github.com/jelder/bownse
  184. github.com/jessewalia/gocode
  185. github.com/jetsanix/go-restful/examples
  186. github.com/jgastal/goplay
  187. github.com/jimmy-go/srest
  188. github.com/jinuljt/goformvalidator
  189. github.com/jmoiron/gowiki
  190. github.com/johnnylee/goutil/httprequest
  191. github.com/jonas747/yagpdb/web
  192. github.com/jonaswouters/goclerk/routers/api/v1/contact
  193. github.com/jonaswouters/goclerk/routers/api/v1/organization
  194. github.com/joncalhoun/sockets
  195. github.com/josephyzhou/go-restful/examples
  196. github.com/joshheinrichs/geosource/server/api
  197. github.com/jsimnz/go-restful/examples
  198. github.com/jskrepnek/goldie
  199. github.com/jstoja/api
  200. github.com/juju/juju/apiserver
  201. github.com/kat-co/juju/apiserver
  202. github.com/kch42/mailremind
  203. github.com/kenng/authboss-sample
  204. github.com/kiwih/heyfyi/heyfyiserver
  205. github.com/kokeroulis/modip/api
  206. github.com/kouphax/dopi
  207. github.com/kyleterry/sufr/app
  208. github.com/lavab/api/utils
  209. github.com/leonardoeloy/persona
  210. github.com/lexandro/go-restful/examples
  211. github.com/liujianping/go-restful/examples
  212. github.com/lixiangzhong/ostent/params
  213. github.com/luismiramirez/ok-go/routes
  214. github.com/lunanode/lobster
  215. github.com/lunanode/lobster/core/support
  216. github.com/m0sth8/go-restful/examples
  217. github.com/makyo/goexplore/gorillastat/handlers
  218. github.com/mantasmatelis/stokr/server
  219. github.com/mathcamp/go-restful/examples
  220. github.com/mattwallington/perfmon
  221. github.com/mbruzek/juju/apiserver
  222. github.com/megadeezl/go-restful/examples
  223. github.com/metcalf/saypi/say
  224. github.com/mikedanese/go-restful/examples
  225. github.com/milliant/go-restful/examples
  226. github.com/mirceamironenco/thesrc/api
  227. github.com/mirceamironenco/thesrc/app
  228. github.com/misrab/bookshare-backend-api/handlers
  229. github.com/misrab/goapi/handlers
  230. github.com/misrab/stellar-watch
  231. github.com/mmcfarland/prc
  232. github.com/moul/sapin/cmd/appspot
  233. github.com/moul/showcase
  234. github.com/mpdroog/rootdev-home
  235. github.com/mpnordland/blgr/posts
  236. github.com/mprobst/blog
  237. github.com/mtojek/go-restful/examples
  238. github.com/muesli/go-restful/examples
  239. github.com/natefinch/juju/apiserver
  240. github.com/nicowernli/gorequest
  241. github.com/niedbalski/juju/apiserver
  242. github.com/niels-s/go-restful/examples
  243. github.com/nilslice/cms/system/db
  244. github.com/ninjasphere/go-daikin-ac
  245. github.com/nsan1129/unframed
  246. github.com/nucleardump/go-wires-old/controllers
  247. github.com/nucleardump/go-wires/models
  248. github.com/nucleartide/go-wires/models
  249. github.com/nucleartide/mango/models
  250. github.com/nullbus/go-restful/examples
  251. github.com/nyc-camp/authenticator/libuser
  252. github.com/ohlinux/go-restful/examples
  253. github.com/ojrac/asphodel-go/methods
  254. github.com/omie/messages/api/v1/messages
  255. github.com/opbk/openbook-landing
  256. github.com/openshift/go-restful/examples
  257. github.com/orian/authboss-sample
  258. github.com/ostrost/ostent/params
  259. github.com/outself/go-restful/examples
  260. github.com/oxfeeefeee/appgo/server
  261. github.com/paceline/autosite-go/autosite
  262. github.com/pavelbyles/Inforce
  263. github.com/perrito666/juju/apiserver
  264. github.com/peterskeide/bones/web/forms
  265. github.com/pgu/go-restful/examples
  266. github.com/phonkee/gopypi
  267. github.com/piinecone/handler
  268. github.com/pingcap/etcdv3-gateway/gateway
  269. github.com/plouc/pompoco
  270. github.com/plouc/pompoco/projects
  271. github.com/plouc/pompoco/users
  272. github.com/pnelson/httpc
  273. github.com/polyglottis/platform/frontend/handle
  274. github.com/porjo/go-restful/examples
  275. github.com/qingchengnus/cab-republic
  276. github.com/quintans/maze
  277. github.com/rande/gonode/core/guard
  278. github.com/rande/gonode/modules/search
  279. github.com/reedobrien/go-restful/examples
  280. github.com/rentongzhang/go-restful/examples
  281. github.com/resc/godel/core/mvc
  282. github.com/retro486/go-asset-repo/assets
  283. github.com/retro486/go-asset-repo/auth
  284. github.com/rhinoman/go-restful/examples
  285. github.com/rickt/slack-flipper
  286. github.com/rickt/slack-team-directory-bot
  287. github.com/rkorkosz/goauth
  288. github.com/rlguarino/urlshortener/frontend
  289. github.com/rm8x/golang-demoserver
  290. github.com/rmulley/jc-hasher/src
  291. github.com/robdimsdale/raspberryfly
  292. github.com/rosenhouse/cnsim/handlers
  293. github.com/runemadsen/ok-go/routes
  294. github.com/runtakun/go-api-generation-sample/templates
  295. github.com/runtakun/go-api-generation-sample/test/gen
  296. github.com/s41nn0n/go-restful/examples
  297. github.com/sabzil/ladybug
  298. github.com/sabzil/ladybug/interfacer
  299. github.com/samertm/githubstreaks
  300. github.com/schleppy/go-restful/examples
  301. github.com/scristofari/golang-poll/api
  302. github.com/scristofari/translate
  303. github.com/sebber/go-wiki-web
  304. github.com/shantanuraj/bookshelf-api
  305. github.com/shawnps/mappuri
  306. github.com/silvasur/mailremind
  307. github.com/sirsean/packhunter/api
  308. github.com/sirsean/textback/sms
  309. github.com/sjhitchner/go-restful/examples
  310. github.com/skatsuta/codetainer
  311. github.com/slspeek/go-restful/examples
  312. github.com/smarterclayton/go-restful/examples
  313. github.com/smithfox/beego/context
  314. github.com/sogko/handler
  315. github.com/sourcegraph/thesrc/api
  316. github.com/sourcegraph/thesrc/app
  317. github.com/sourcegraph/vcsstore/server
  318. github.com/spuranam/trifles/mpush
  319. github.com/squishyent/go-restful/examples
  320. github.com/stackdocker/cloudfoundry-broker-kubernetes-docker/api
  321. github.com/stgraber/juju/apiserver
  322. github.com/straumur/straumur
  323. github.com/stuart-warren/go-restful/examples
  324. github.com/studygolang/studygolang/src/logic
  325. github.com/stunndard/lobster
  326. github.com/sunfmin/batchbuy
  327. github.com/tehleach/go-restful/examples
  328. github.com/theplant/gmo
  329. github.com/timtadh/cc-survey/models
  330. github.com/timtadh/cc-survey/views
  331. github.com/tj/auth/dev
  332. github.com/tj/auth/password
  333. github.com/topher200/forty-thieves/handlers
  334. github.com/tortuoise/fiandsri
  335. github.com/tortuoise/wattwerks
  336. github.com/trendmicro/gop
  337. github.com/turnkey-commerce/go-ping-sites/controllers
  338. github.com/ubccr/mokey
  339. github.com/uvelichitel/nezabu/go.graf
  340. github.com/vikstrous/go-restful/examples
  341. github.com/vimdude/go-restful/examples
  342. github.com/wallyworld/juju-snap/apiserver
  343. github.com/wallyworld/juju/apiserver
  344. github.com/wanghe4096/go-restful/examples
  345. github.com/wangyibin/echoswg
  346. github.com/wangyibin/gonext
  347. github.com/wawandco/gontact/handlers
  348. github.com/wcl48/go-api-generation-sample/templates
  349. github.com/wisedog/ladybug
  350. github.com/wisedog/ladybug/interfacer
  351. github.com/wolfeidau/assembly/api
  352. github.com/wolfeidau/authinator/api
  353. github.com/wolfeidau/proxy-auth
  354. github.com/wongak/go-restful/examples
  355. github.com/wuub/go-restful/examples
  356. github.com/wvdeutekom/GoQuotes/api
  357. github.com/xjdrew/goweb/app/controllers
  358. github.com/zach-klippenstein/goregen-demo
  359. github.com/zdebeer99/webapp
  360. github.com/zdebeer99/weblib
  361. github.com/zenithar/api/utils
  362. github.com/zethra/Name-Tag-Auto-Printing-Go/src
  363. github.com/zhymin77/go-restful/examples
  364. github.com/zond/goaeoas
  365. github.com/zykzhang/practice/pacpackge/gorilla-schema
  366. gopkg.in/Nainterceptor/go-restful.v1/examples
  367. gopkg.in/emicklei/go-restful.v1/examples
  368. gopkg.in/evq/go-restful.v1/examples
  369. gopkg.in/goanywhere/web.v0/form
  370. gopkg.in/jabong/go-restful.v1/examples
  371. gopkg.in/jimmy-go/srest.v0
  372. sourcegraph.com/sourcegraph/sourcegraph/app/internal/schemautil
  373. sourcegraph.com/sourcegraph/sourcegraph/errcode
  374. sourcegraph.com/sourcegraph/sourcegraph/httpapi
  375. sourcegraph.com/sourcegraph/sourcegraph/pkg/errcode
  376. sourcegraph.com/sourcegraph/sourcegraph/pkg/handlerutil
  377. sourcegraph.com/sourcegraph/sourcegraph/ui
  378. sourcegraph.com/sourcegraph/sourcegraph/util/errcode
  379. sourcegraph.com/sourcegraph/sourcegraph/util/handlerutil
  380. sourcegraph.com/sourcegraph/thesrc/api
  381. sourcegraph.com/sourcegraph/thesrc/app
  382. sourcegraph.com/sourcegraph/vcsstore/server
  383. src.sourcegraph.com/apps/tracker
  384. src.sourcegraph.com/sourcegraph/app/internal/schemautil
  385. src.sourcegraph.com/sourcegraph/errcode
  386. src.sourcegraph.com/sourcegraph/httpapi
  387. src.sourcegraph.com/sourcegraph/ui
  388. src.sourcegraph.com/sourcegraph/util/handlerutil

Imported only in test by 5 package(s)

  1. github.com/PuerkitoBio/httpparms
  2. github.com/keegancsmith/go-sourcegraph/sourcegraph
  3. github.com/marciol/go-sourcegraph/sourcegraph
  4. github.com/rande/gonode/modules/api
  5. github.com/salsita/go-sprintly/sprintly