This is a creation in Article, where the information may have evolved or changed.
In the development process will often meet a number of problems, and now the summary and the solution to record.
1, the session set problems
When the session is set, there are integers, string types, which are used when connecting:
Fmt. Sprintf () + connection string, no format, direct stitching will error
this.SetSession("userLogin", fmt.Sprintf("%d", users.Id)+"||"+users.Username+"||"+users.Avatar)
2, Session access issues
After setting the session, you get the
this.GetSession("userLogin")
If the value of the Userlogin is integer, no error will be shown; If you press the above (first), the integer and string concatenation, will be error
Can be used:
this.GetSession("userLogin").(string)
. (string) There are many types, if you get the session error, according to their actual situation replaced. (int), etc.
3, long integer conversion problem
Digital Long integer string conversion growth integer, can be converted to integer type, then to grow the integral type
Such as:
userid, _ := strconv.Atoi(tmp[0])longid := int64(userid)
4, long integer digital return to the front-end JS, numerical overflow problem
In the case of commit or other operations, often return to the JSON format to the front end, for long integer numbers, JS processing will overflow
The long integer value returned must be FMT. Sprintf ("%d", ID) format processing, so that the front-end JS does not have to process ~
this.Data["json"] = map[string]interface{}{"code": 1, "message": "项目信息添加成功", "id": fmt.Sprintf("%d", id)}
5. String segmentation into Map
In the project will often deal with some such as "Lock,tom,jerry, Li Bai", now want to convert to map:
names := strings.Split("6565243443941654055,4365243443941654055,1454243443941654055,1465243443941654034", ",")var namesmap = make(map[int]int64)for i, v := range ccids { names, _ := strconv.Atoi(v) namesmap[i] = int64(names)}fmt.Println(namesmap)
6, multi-conditional transfer parameters, query
In the model often write some common methods, will be based on different criteria for SQL search
condArr := make(map[string]string)condArr["projectid"] = idstrcondArr["status"] = statuscondArr["type"] = stype
Passing Condarr in a method is possible, and the definition of the method may be
func ListProjectTask(condArr map[string]string, page int, offset int) (num int64, err error, ops []ProjectsTask) {}
7, Beego pagination
To reference:
"github.com/astaxie/beego/utils/pagination"
Specific as follows:
countTask := CountTask(condArr)paginator := pagination.SetPaginator(this.Ctx, offset, countTask)_, _, tasks := ListProjectTask(condArr, page, offset)this.Data["tasks"] = tasksthis.Data["paginator"] = paginator
At present, the first summary of some basic problems, if you meet, continue to append ~