Collections and Composite Types
Appending to Slices
Slices can grow with append, returning an updated slice value.
Appending to Slices
slices_append.go
package main
import "fmt"
func main() {
var nextScore =
scores := []int{82, 91}
updated := append(scores, nextScore)
fmt.Println("before=", scores)
fmt.Println("after=", updated)
fmt.Println("length=", len(updated))
}
package main
import "fmt"
func main() {
var nextScore =
scores := []int{82, 91}
updated := append(scores, nextScore)
fmt.Println("before=", scores)
fmt.Println("after=", updated)
fmt.Println("length=", len(updated))
}
package main
import "fmt"
func main() {
var nextScore =
scores := []int{82, 91}
updated := append(scores, nextScore)
fmt.Println("before=", scores)
fmt.Println("after=", updated)
fmt.Println("length=", len(updated))
}
append
`append` returns a slice that includes the added value.