Append Item to Slice in Golang

Create new folder named src. In src folder, create new file named main.go as below:

package main

import (
	"fmt"
)

func main() {

	var a = [5]int{4, 1, -2, 9, 10}

	fmt.Println("a: ", a)
	fmt.Println("Length of a: ", len(a))

	slice1 := a[0:2]
	fmt.Println("slice 1: ", slice1)

	slice1 = append(slice1, 25)
	fmt.Println("slice 1: ", slice1)
	fmt.Println("a: ", a)
	fmt.Println("Length of a: ", len(a))

	slice1 = append(slice1, 100, 200, 300)
	fmt.Println("slice 1: ", slice1)
	fmt.Println("a: ", a)
	fmt.Println("Length of a: ", len(a))

}




Open Terminal windows in Visual Studio Code and run command line: go run main.go

a:  [4 1 -2 9 10]
Length of a:  5
slice 1:  [4 1]
slice 1:  [4 1 25]
a:  [4 1 25 9 10]
Length of a:  5
slice 1:  [4 1 25 100 200 300]
a:  [4 1 25 9 10]
Length of a:  5