error!!!
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 want to achieve this result!!!
[{1 aaa [https//you...,https//you...],2 bbb [https/you...]}]
I want to get a movie that is linked by category association by slice
-----------------------------PS ---------------------------------
This is what I wanted to do!!!
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
}
Thanks everyone
Categorydata structure that you've declared. That is, it looks like what you want is each category with a slice of the associated movies' urls, but what you have in the theCategory.Moviesfield is a slice ofMovievalues, which are structs with more fields than just theUrl... So which is it?