- My first project after I've started the Go4Noobs Repo
- This project will be a Request Validator
- My inspiration is the Laravel Request Validator
- Define Folder Structure
- Create a Package
- Call one Function
- Maybe like this:
-
func Validate(reqStruct interface{}, req *http.Request) { // so it would only be called and sended those // params dec := json.NewDecoder(req.Body) dec.Decode(reqStruct) /* ... something like this ... */ }
- POST Validation
- Simple
- Required
- Type
- Length
- Medium Level
- Unique
- Regex
- Simple
- Database Connection
- Based on:
- go-request-validator/ - Package Root Dir
- test/ - Folder for tests
- tools/ - Helpers, global func
- pkg/ - Packages that can be used by everything
- config/ - App config files
- cmd/ - Main Applications
- Receiving json data
-
package main import ( "log" "net/http" "encoding/json" ) type test_struc struct { // varName varType `json:"keyName"` Name string `json:"name"` Email string `json:"email"` Age int `json:"age"` } // Func Handler func getReq(w http.ResponseWriter, req *http.Request) { dec := json.NewDecoder(req.Body) var t test_struc err := dec.Decode(&t) if(err != nil) { panic(err) } log.Println(t) } func main() { // http.HandleFunc("/endpoint", func) http.HandleFunc("/", getReq) // http.ListenAndServe(":port", nil) http.ListenAndServe(":8080", nil) }
- Sending:
-
{ "name": "Rafael Breno de Vasconcellos Santos", "email": "[email protected]", "age": 20 }
- The struct t will be:
- {Rafael Breno de Vasconcellos Santos [email protected] 20}