Home > database >  Get a Bitmap type filter queried from URL in Golang
Get a Bitmap type filter queried from URL in Golang

Time:05-31

I'm working on a large Go web app built with Gin framework. For one of the APIs requesting user info, there is a filter parameter whose input type is bitmap, denoting whether user is online or offline:

// SearchUsers API parameter: filterUser
// filterUser is a bitmap
Online: 1 << 0
Offline: 1 << 1


// Values:
0 -> not used
1 -> online
2 -> offline
3 -> all (online, offline)

Right now I'm parsing all the API parameters using Request.URL.Query. I was wondering if this works the same for bitmap type as well? Do I need to create a []byte to process the input and then convert it to integer for DB queries?

func (h *HandleManager) SearchUsers() gin.HandlerFunc {
    return func(c *gin.Context) {
        // ...
        q := c.Request.URL.Query()
        filterUsers := q.Get("filterUser")
        if filterUsers != "" {
            filterUsersByte := []byte(filterUsers)
        } 

CodePudding user response:

q.Get() returns a string value holding (likely, depends on the client) the decimal representation of your bitmap. Converting it to []byte will give you the UTF-8 encoded bytes of the string. This is not what you want.

Use strconv.Atoi() to parse the number from the string, e.g.:

filterUsers := q.Get("filterUser")
bitmap, err := strconv.Atoi(filterUsers)
if err != nil {
    // Handle error
}
// Use bitmap
  • Related