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

huangapple go评论95阅读模式
英文:

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

问题

我想要像处理 JSON 或 XML 一样处理 x-www-form-urlencoding 的编码和解码。是否有现有的包可以实现这个功能,或者是否有关于如何自己实现这个功能的文档?

英文:

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?

答案1

得分: 23

gorilla/schema 是一个受欢迎且维护良好的库:

例如:

func FormHandler(w http.RequestWriter, r *http.Request) {
    
    err := r.ParseForm()
    if err != nil {
         // 处理错误
    }
    person := new(Person) // Person 是一个结构体类型
    decoder := schema.NewDecoder()
    
    err = decoder.Decode(person, r.Form)
    if err != nil {
         // 处理错误
    }

}

goforms 也是一个可选的库。

更新于2015年5月23日:

  • gorilla/schema 仍然是我认为最受支持的映射到结构体的库之一,常用于处理 POST 表单值。
  • goji/param 也相当可靠,并具有许多相同的功能。
  • mholt/binding 在我看来功能更丰富,但 API 稍微复杂一些。

我已经使用 gorilla/schema 几年了,没有遇到任何重大问题。我通常与 vala 一起使用,用于在数据进入数据库之前验证输入(非空、过短、过长等)。

英文:

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.

答案2

得分: 8

我刚刚找到了 https://github.com/ajg/form,这正是我在寻找的东西。还有 https://github.com/gorilla/schema 用于严格解码,以及 https://github.com/google/go-querystring 用于严格编码。

英文:

I just found https://github.com/ajg/form which is exactly what I was looking for. There is also https://github.com/gorilla/schema for strictly decoding and https://github.com/google/go-querystring for strictly encoding.

答案3

得分: 6

net/url似乎可以很好地处理这个问题:

package main

import (
   "fmt"
   "net/url"
)

func main() {
   {
      m := url.Values{
         "CR": {"\r"}, "LF": {"\n"},
      }
      s := m.Encode()
      fmt.Println(s) // CR=%0D&LF=%0A
   }
   {
      s := "CR=%0D&LF=%0A"
      m, e := url.ParseQuery(s)
      if e != nil {
         panic(e)
      }
      fmt.Printf("%q\n", m) // map["CR":["\r"] "LF":["\n"]]
   }
}
英文:

net/url seems to handle this just fine:

package main

import (
   "fmt"
   "net/url"
)

func main() {
   {
      m := url.Values{
         "CR": {"\r"}, "LF": {"\n"},
      }
      s := m.Encode()
      fmt.Println(s) // CR=%0D&LF=%0A
   }
   {
      s := "CR=%0D&LF=%0A"
      m, e := url.ParseQuery(s)
      if e != nil {
         panic(e)
      }
      fmt.Printf("%q\n", m) // map["CR":["\r"] "LF":["\n"]]
   }
}

答案4

得分: 2

https://github.com/google/go-querystring很好,但不支持映射(和映射的切片)。

我开始了https://github.com/drewlesueur/querystring来支持映射。
(它还不支持结构体,但欢迎提交拉取请求)。

英文:

https://github.com/google/go-querystring is good, but doesn't support maps (and slices of maps).

I started https://github.com/drewlesueur/querystring for map support.
(It doesn't support structs yet, but pull requests welcome).

huangapple
  • 本文由 发表于 2014年3月23日 01:46:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/22581248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定