使用fmt.Sprintf拼接SQL
实例代码
func Select(ids []string) string {
idStr := strings.Join(ids, "','")
sqlText := "select id, name, age, created_at, updated_at from car where id ('%s') limit %d"
sqlText = fmt.Sprintf(sqlText, idStr, 20)
return sqlText
}
func main() {
sql := Select([]string{"1", "2", "3"})
fmt.Println(sql)
}
select id, name, age, created_at, updated_at from car where id ('1','2','3') limit 20
有疑问加站长微信联系(非本文作者)