Created
October 12, 2017 19:04
-
-
Save cecyc/b816ca3738cd6f56ce755109e94f93af to your computer and use it in GitHub Desktop.
How to append objects into a slice in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type Doggo struct { | |
Name string | |
Age int | |
} | |
func main() { | |
//Create empty slice of Doggos | |
var doggos []Doggo | |
//Instantiate a new Doggo | |
d := Doggo{"Rusty", 10} | |
//Append the Doggo to the slice | |
doggos = append(doggos, d) | |
fmt.Print(doggos) | |
//Instantiate another Doggo | |
d = Doggo{"Baloo", 7} | |
//Append new Doggo to slice | |
doggos = append(doggos, d) | |
fmt.Print(doggos) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment