Go Programming Language Slice

Go Programming Language Slice

Using slice, in Go programming array can be defined without giving array length.

make function is used to set the array length.

Go slice Example

This Go code is used to explain how to declare the array without length using slice, inserting data in array and reading array data to print.

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
                                                                           	 
	var numbers[] int                                                      	 
	var name[] string                                                      	 
                                                                           	 
	numbers = make([]int, 4)                                               	 
	name = make([]string, 4)                                               	 
                                                                           	 
	fmt.Println("Inserting slice data")                                    	 
	// insert array data                                                   	 
	for i:=0;i<4;i++ {                                                     	 
    	numbers[i] = i+1                                                   	 
    	name[i] =  fmt.Sprintf("Test Name%d", i)                           	 
	}                                                                      	 
                                                                           	 
	fmt.Println("Print slice data")                                        	 
	// Prints array data                                                   	 
	for i:=0;i<4;i++ {                                                     	 
    	fmt.Printf("number %d: %d\n", i, numbers[i])                       	 
    	fmt.Printf("name %d: %s\n", i, name[i])                            	 
	}                                                                      	 
}
Output:
$ go build slice.go
$ ./slice
Inserting slice data
Print slice data
number 0: 1
name 0: Test Name0
number 1: 2
name 1: Test Name1
number 2: 3
name 2: Test Name2
number 3: 4
name 3: Test Name3

Go Two Dimensional slice

Syntax:
var matrix[][] int

make function is used to set the array length.

Go Two Dimensional slice Example

Two dimensional array without providing array length.

Below code explains how to declare two dimensional slice, insert values and reading slice values.

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
                                                                           	 
	var matrix[][] int                                                     	 
                                                                           	 
	matrix = make([][]int, 3*3)                                            	 
                                                                           	 
	fmt.Println("Inserting slice matrix data")                             	 
	// insert slice matrix data                                                   	 
	for i:=0;i<3;i++ {                                                     	 
    	matrix[i] = make([]int, 3)                                         	 
    	for j:=0;j<3;j++ {                                                 	 
        	matrix[i][j] = i*j                                             	 
    	}                                                                  	 
	}                                                                      	 
                                                                           	 
	fmt.Println("Print slice matrix data")                                 	 
	// Prints slice matrix data                                                   	 
	for i:=0;i<3;i++ {                                                     	 
    	for j:=0;j<3;j++ {                                                 	 
        	fmt.Printf("%d",matrix[i][j])                                  	 
        	if j!=2 {                                                      	 
            	fmt.Printf("\t")                                           	 
        	}                                                              	 
    	}                                                                  	 
    	fmt.Printf("\n")                                                   	 
	}                                                                      	 
}  
Output:
$ go build slice.go
$ ./slice
Inserting slice matrix data
Print slice matrix data
0    0    0
0    1    2
0    2    4