Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a package to marshal in and out of x-www-form-urlencoding in golang

I would like to marshal in and out of x-www-form-urlencoding similar to how you can do it with json or xml. Is there an existing package to do this, or are there any documents on how to implement one myself if none exist?

like image 912
placeybordeaux Avatar asked Mar 22 '14 17:03

placeybordeaux


People also ask

How do you struct a marshal in Golang?

In Golang, struct data is converted into JSON and JSON data to string with Marshal() and Unmarshal() method. The methods returned data in byte format and we need to change returned data into JSON or String. In our previous tutorial, we have explained about Parsing JSON Data in Golang.

What is the difference between Marshal and Unmarshal in Golang?

Unmarshal is the contrary of marshal. It allows you to convert byte data into the original data structure. In go, unmarshaling is handled by the json.

How do I encode URL in Golang?

URL Encoding or Escaping a String in Go Go provides the following two functions to encode or escape a string so that it can be safely placed inside a URL: QueryEscape(): Encode a string to be safely placed inside a URL query string. PathEscape(): Encode a string to be safely placed inside a URL path segment.


1 Answers

gorilla/schema is popular and well maintained:

e.g.

func FormHandler(w http.RequestWriter, r *http.Request) {      err := r.ParseForm()     if err != nil {          // handle error     }     person := new(Person) // Person being a struct type     decoder := schema.NewDecoder()      err = decoder.Decode(person, r.Form)     if err != nil {          // handle error     }  } 

goforms is also an alternative.

Update May 23rd 2015:

  • gorilla/schema is still my pick as one of the most-supported map-to-struct packages, with POST form values being a common use-case.
  • goji/param is also fairly solid and has many of the same features.
  • mholt/binding is a little more feature packed at the (IMO) expense of a slightly more complex API.

I've been using gorilla/schema for a couple of years now and haven't had any major issues with it. I use it in conjunction with vala for validating inputs (not nil, too short, too long, etc) before they hit the DB.

like image 82
elithrar Avatar answered Sep 23 '22 00:09

elithrar