ошибка !!!
Scan error on column index 1, name "url": unsupported Scan, storing driver.Value type []uint8 into type *[]handle.Movie
https://gyazo.com/7532a1c3793c892e721054998865609d
https://gyazo.com/278066e6da16f13cd9c56874beb71026
type Movie struct {
ID int
Url string
CategoryID uint
}
type Category struct {
ID int
Name string
Movies []Movie
}
func Connected() []Category {
db := ConnectDB()
defer db.Close()
//sql
query := `SELECT c.name,m.url FROM categories c left join movies m on c.id = m.category_id`
rows, err := db.Query(query)
if err != nil {
log.Fatal(err)
}
var sli []Category
var v1 Category
for rows.Next() {
if err := rows.Scan(&v1.Name, &v1.Movies); err != nil {
log.Fatal(err)
}
sli = append(sli, v1)
}
fmt.Println(sli[0].Movies)
return sli
}
I хочу достичь этого результата !!!
[{1 ааа [https // you ..., https // you ...], 2 bbb [https / you ...]}]
Я хочу получить mov ie, который связан ассоциацией категорий срезом
----------------------- ------ PS ---------------------------------
Это то, что я хотел сделать !!!
func Connected() []Category {
db := ConnectDB()
defer db.Close()
//sql
query := `SELECT c.id, c.name, m.id, m.url FROM categories c left join movies m on c.id = m.category_id ORDER BY c.id ASC`
rows, err := db.Query(query)
if err != nil {
log.Fatal(err)
}
var sli []Category
var c Category
var m Movie
for rows.Next() {
if err := rows.Scan(&c.ID, &c.Name, &m.ID, &m.Url); err != nil {
log.Fatal(err)
}
m.CategoryID = c.ID
l := len(sli)
if l > 0 && sli[l-1].ID == c.ID {
sli[l-1].Movies = append(sli[l-1].Movies, m)
} else {
if len(c.Movies) != 0 {
c.Movies = remove(c.Movies, c.Movies[0])
}
c.Movies = append(c.Movies, m)
sli = append(sli, c)
}
}
return sli
}
func remove(ints []Movie, search Movie) []Movie {
result := []Movie{}
for _, v := range ints {
if v != search {
result = append(result, v)
}
}
return result
}
Спасибо всем