mholt/binding là dự án mã nguồn mở trên GitHub, viết chủ yếu bằng Go, với 792 sao. Reflectionless data binding for Go's net/http (not actively maintained)
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Moves data binding, validation, and error handling out of your application's handler
Reads Content-Type to deserialize form, multipart form, and JSON data from requests
No middleware: just a function call
Usable in any setting where net/http is present (Negroni, gocraft/web, std lib, etc.)
No reflection
Usage example
package main
import (
"fmt"
"net/http"
"github.com/mholt/binding"
)
// First define a type to hold the data
// (If the data comes from JSON, see: http://mholt.github.io/json-to-go)
type ContactForm struct {
User struct {
ID int
}
Email string
Message string
}
// Then provide a field mapping (pointer receiver is vital)
func (cf *ContactForm) FieldMap(req *http.Request) binding.FieldMap {
return binding.FieldMap{
&cf.User.ID: "user_id",
&cf.Email: "email",
&cf.Message: binding.Field{
Form: "message",
Required: true,
},
}
}
// Now your handlers can stay clean and simple
func handler(resp http.ResponseWriter, req *http.Request) {
contactForm := new(ContactForm)
if errs := binding.Bind(req, contactForm); errs != nil {
http.Error(resp, errs.Error(), http.StatusBadRequest)
return
}
fmt.Fprintf(resp, "From: %d\n", contactForm.User.ID)
fmt.Fprintf(resp, "Message: %s\n", contactForm.Message)
}
func main() {
http.HandleFunc("/contact", handler)
http.ListenAndServe(":3000", nil)
}
Multipart/form-data usage example
package main
import (
"bytes"
"fmt"
"github.com/mholt/binding"
"io"
"log"
"mime/multipart"
"net/http"
)
// We expect a multipart/form-data upload with
// a file field named 'data'
type MultipartForm struct {
Data *multipart.FileHeader `json:"data"`
}
func (f *MultipartForm) FieldMap(req *http.Request) binding.FieldMap {
return binding.FieldMap{
&f.Data: "data",
}
}
// Handlers are still clean and simple
func handler(resp http.ResponseWriter, req *http.Request) {
multipartForm := new(MultipartForm)
if errs := binding.Bind(req, multipartForm); errs != nil {
http.Error(resp, errs.Error(), http.StatusBadRequest)
return
}
// To access the file data you need to Open the file
// handler and read the bytes out.
var fh io.ReadCloser
var err error
if fh, err = multipartForm.Data.Open(); err != nil {
http.Error(resp,
fmt.Sprint("Error opening Mime::Data %v", err),
http.StatusInternalServerError)
return
}
defer fh.Close()
dataBytes := bytes.Buffer{}
var size int64
if size, err = dataBytes.ReadFrom(fh); err != nil {
http.Error(resp,
fmt.Sprint("Error reading Mime::Data %v", err),
http.StatusInternalServerError)
return
}
// Now you have the attachment in databytes.
// Maximum size is default is 10MB.
log.Printf("Read %v bytes with filename %s",
size, multipartForm.Data.Filename)
}
func main() {
http.HandleFunc("/upload", handler)
http.ListenAndServe(":3000", nil)
}
You can test from CLI using the excellent httpie client
http -f POST localhost:3000/upload data@myupload
Custom data validation
You may optionally have your type implement the binding.Validator interface to perform your own data validation. The .Validate() method is called after the struct is populated.
func (cf ContactForm) Validate(req *http.Request) error {
if cf.Message == "Go needs generics" {
return binding.Errors{
binding.NewError([]string{"message"}, "ComplaintError", "Go has generics. They're called interfaces.")
}
}
return nil
}
Binding custom types
For types you've defined, you can bind form data to it by implementing the Binder interface. Here's a contrived example:
If you can't add a method to the type, you can still specify a Binder func in the field spec. Here's a contrived example that binds an integer (not necessary, but you get the idea):
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/mholt/binding là nguồn chính thức.
mholt/binding có bao nhiêu sao?
mholt/binding có 792 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/mholt/binding. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
mholt/binding có phải mã nguồn mở không?
Có — mholt/binding phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/mholt/binding.
mholt/binding có trang demo không?
Dự án có trang chủ ở http://mholt.github.io/binding. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
mholt/binding là gì?
mholt/binding (mholt/binding) là dự án Go trên GitHub. Theo mô tả gốc: Reflectionless data binding for Go's net/http (not actively maintained)
Đọc đầy đủ README ở tab phía trên.
binding có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về binding.