go - What is the correct way to save post data (with integer and string values) in the database golang? -


i have code in golang below :

     package main      import (         "github.com/gin-gonic/gin"         "gopkg.in/mgo.v2"         "gopkg.in/mgo.v2/bson"         "log"         "time"     )      func main() {         router := gin.default()         router.post("/save-address", saveaddress)         router.run()     }      func saveaddress(c *gin.context){         var err error         conditions := bson.m{}         c.request.parseform()         key, _ := range c.request.postform {             if key != "id"{                 conditions[key] = c.postform(key)             }         }         conditions["_id"] = 1         mongosession := connectdb()          sessioncopy := mongosession.copy()         defer sessioncopy.close()          getcollection := mongosession.db("bformssettings").c("address")         err = getcollection.insert(conditions)         if err != nil{             println(err)         }else{             println("data saved successfully!!!")         }     }     func connectdb() (mongosession *mgo.session) {         mongodbdialinfo := &mgo.dialinfo{             addrs:    []string{"localhost:27017"},             timeout:  60 * time.second,             database: "bformssettings",         }          mongosession, err := mgo.dialwithinfo(mongodbdialinfo)         if err != nil {             log.fatalf("createsession: %s\n", err)         }          mongosession.setmode(mgo.monotonic, true)          return mongosession     } 

when run code, data saved in database this:

    { "_id" : 1, "customer_id" : "3", "address" : "chicago, il", "city" : "chicago", "state" : "il", "zipcode" : "60647" }

where customer_id integer value saved in database string.

there 1 way can convert customer id string integer before data saved in database.

is there way save data is, submitted(like integer values saved integer)?

if @ saved document, can see it's _id property number (and not string), yes, possible.

the reason why end customer_id property being of type string because document save (conditions variable) holds string value customer_id property. , of type string because filled return value of context.postform() returns form value string.

if want integer in database, convert go integer before setting in conditions. may use strconv.atoi() example.

for key, _ := range c.request.postform {     value := c.postform(key)     if key == "customer_id" {         if id, err := strconv.atoi(value); err != nil {             // not valid number, handle error         } else {             conditions[key] = id         }     } else {         if key != "id"{             conditions[key] = value         }     } } 

you have define somehow fields supposed hold integer values. showed way single field. if have multiple of them, may list them in slice, , use single for loop detect / handle all; or better, put them in map acts set, , can detect these fields without for loop.

another option create struct type modeling form input data, , use library gorilla's schema unmarshal struct in type-aware manner.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -