Golang use array values in db query to filter records

1.6k Views Asked by At

I have list of array int64 values

ids = [{1} {2} {3}]

I want to use the above array in db query to filter out the records where ID is not in above ids.

SELECT * from table where id not in (1,2,3);

I tried many ways to do but failing to make the query string.

1

There are 1 best solutions below

0
On

I have created a sample scenario as follows :

func main() {
    ids := []int{1, 2, 3}

    var tmp []string

    for _, v := range ids {
        tmp = append(tmp, fmt.Sprint(v))
    }

    query := "SELECT * from table where id not in (" + strings.Join(tmp, ",") + ")"
    fmt.Println(query)

}

OR

You can run it in go playground link