[GO] converting argument $ 1 type: unsupported type [] string, a slice of string

Overview

This is the first post in my first year as an engineer. When I was operating the DB using sqlx, I was troubled by the following error, so I will record it. I would be grateful if you could comment and give me a bad idea.

converting argument $1 type: unsupported type []string, a slice of string

scenario

When I was trying to perform an operation using the IN condition in the WHERE clause of the SELECT statement, I got an error. This time, I will write an operation to extract USER who can use go and python from the following USER table.

Name Skill
Ichiro C
Jiro C#
Saburo Java
Shiro python
Goro go
Rokuro Ruby
Shichiro go

Various tools

Bad code

	query := "SELECT Name,Skill  FROM USER WHERE Skill IN (?,?);"
	searchSkills := []string{"go", "python"}

	var records []User
	err := db.Select(&records, query, searchSkills)
	if err != nil {
		log.Fatal(err)
	}

	//converting argument $1 type: unsupported type []string, a slice of string

Pass [] string as a parameter to db.Select. I wonder if it will work because it compiles, but I get an error in the subject. By the way, if the third argument is searchSkills ..., the compilation will not pass.

What happened

I somehow understand that the type passed as an argument as an error message is not good. For the time being, read the document. https://godoc.org/github.com/jmoiron/sqlx#DB.Select

Since the third argument was a variadic argument of ʻinterface {}, let's expand [] interface {}` and throw it.

	query := "SELECT Name,Skill FROM USER WHERE Skill IN (?,?)"
	// searchSkills := []string{"go", "python"}
	searchSkills := []interface{}{"go", "python"}

	var records []User
	err := db.Select(&records, query, searchSkills...)
	if err != nil {
		log.Fatal(err)
	}

	// Name:Jiro,Skill:go
	// Name:Saburo,Skill:go
	// Name:Goro,Skill:python

You can do what you want with less error. But it's not very smart ... It is not good that you have to change the number of ? Depending on the extraction conditions. It feels strange to define it with [] interface {}.

sqlx.In There was something convenient. You can make it smarter by using the In () function provided by sqlx.

func In(query string, args ...interface{}) (string, []interface{}, error) https://godoc.org/github.com/jmoiron/sqlx#In

	queryBase := "SELECT Name,Skill FROM USER WHERE Skill IN (?)"
	searchSkills := []string{"go", "python"}

	query, param, err := sqlx.In(queryBase, searchSkills)
	if err != nil {
		log.Fatal(err)
	}
	var records []User
	err = db.Select(&records, query, param...)
	if err != nil {
		log.Fatal(err)
	}

	// Name:Jiro,Skill:go
	// Name:Saburo,Skill:go
	// Name:Goro,Skill:python

By passing the underlying SQL statement to the In () function and the slice you want to use as a parameter, it will create a query that adjusts the number of placeholders according to the number of elements in the slice. After that, if you pass it to db.Select () together with param which became[] interface {}, you can search using In condition. In this case, it seems that there is no need to worry about the number of extraction conditions and the type of parameters. Great.

By the way, if you pass a slice with 0 elements to the In () function, the following error will occur, so be careful.

empty slice passed to 'in' query

reference

golang: sql: converting argument $1 type: unsupported type []int32, a slice of int32

Recommended Posts

converting argument $ 1 type: unsupported type [] string, a slice of string
Cut a part of the string using a Python slice
Format when passing a long string as an argument of python
Timezone specification when converting a string to datetime type in python
String conversion of a list containing numbers
[python] [meta] Is the type of python a type?
Define a custom argument type with click (Example: Implementation of a type that makes a string eval an optional value)
A memorandum of python string deletion process
python string slice
Python2 string type
Python # string type
Get the variable name of the variable as a character string.
Convert a slice object to a list of index numbers
How to write a list / dictionary type of Python3
# Function that returns the character code of a string
Basics of Python learning ~ What is a string literal? ~
When converting a string (a-> b) in Python, which is faster, if statement or dictionary type?