i have an struct & array slice
type Book struct {
bookName string
category string
creator string
}
var books = []Book{
{bookName: "study go", category: "programming", creator: "steve"},
{bookName: "study html", category: "programming", creator: "jobs"},
}
func main() {
//how to add data to index[0] or front of books
}
CodePudding user response:
How about this?
package main
import "fmt"
func test() {
books = append([]Book{{
bookName: "study c ",
category: "programing",
creator: "tmp",
}}, books...)
fmt.Println(books)
}
CodePudding user response:
Your question is not clarified. I have tried to give you an answer. I combined the first index in the books
variable with other data in the new
variable.
package main
import "fmt"
type Book struct {
bookName string
category string
creator string
}
func main() {
var books = []Book{
{bookName: "study go", category: "programming", creator: "steve"},
{bookName: "study html", category: "programming", creator: "jobs"},
}
var new = []Book{
{bookName: "study css", category: "Styling", creator: "john"},
}
new = append(books[0:2], new[0])
fmt.Println(new)
}
Result
[{study go programming steve} {study html programming jobs} {study css Styling john}]