Skip to content

Instantly share code, notes, and snippets.

@cecyc
Created October 12, 2017 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cecyc/b816ca3738cd6f56ce755109e94f93af to your computer and use it in GitHub Desktop.
Save cecyc/b816ca3738cd6f56ce755109e94f93af to your computer and use it in GitHub Desktop.
How to append objects into a slice in Go
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