Beego-go language encountered some small problems in the development process summary

Source: Internet
Author: User
Tags integer numbers sprintf
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 ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.