I am having trouble calling the append
function in Go
type Dog struct {
color string
}
type Dogs []Dog
I want to append "Dog" into "Dogs".
I tried doing this
Dogs = append(Dogs, Dog)
But I get this error
First argument to append must be slice; have *Dogs
Edit: Also, if I want to check if this Dog contains the color "white", for example. How would I call this?
if Dog.color.contains("white") {
//then append this Dog into Dogs
}
As friends says it should not be a type, here is example can be helpful:
// Create empty slice of struct pointers.
Dogs := []*Dog{}
// Create struct and append it to the slice.
dog := new(Dog)
dog.color = "black"
Dogs = append(Dogs, dog)
Dogs is a type not a variable, you probably meant to:
var Dogs []Dog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With