MOCKSTACKS
EN
Questions And Answers

More Tutorials








Go Slices

Slice in Go are a key data type, similar to arrays but more powerful and flexible. slices are used to store multiple values that has the same type, however you do not need to specify the length, it can grow and shrink as you fit so it is a dynamically-sized.

In Go you can create slices in several ways below is the most commonly used to create slices:

  • Declare Slice using new Keyword
  • Declare Slice using Make
  • Declare a slice from an array
  • Declare a slice using the []datatype{values} format


Declare Slice using new Keyword

In Go you can declare a slice using new keyword followed by capacity in square brackets then type of elements the slice will hold.

Example


package main

import (
	"fmt"
	"reflect"
)

func main() {
	var intSlice = new([50]int)[2:5]
//this will return the type of object
	fmt.Println(reflect.ValueOf(intSlice).Kind()) 
// getting capacity and length
	fmt.Printf("intSlice \tLength: %v \tCapacity: %v\n", len(intSlice), cap(intSlice)) 
	fmt.Println(intSlice)
}

Output

slice
intSlice Length: 10 Capacity: 50
[0 0 0 0 0]


Declare Slice using Make Syntax

In Go you can declare a slice the built-in function make. When you use make, one option you have is to specify the length of the slice. When you just specify the length, the capacity of the slice is the same.

Example


package main

import (
	"fmt"
	"reflect"
)

func main() {
	var intSlice = make([]int, 10)        // when length and capacity is same
	var strSlice = make([]string, 10, 20) // when length and capacity is different

	fmt.Printf("intSlice \tLen: %v \tCap: %v\n", len(intSlice), cap(intSlice))
	fmt.Println(reflect.ValueOf(intSlice).Kind())

	fmt.Printf("strSlice \tLen: %v \tCap: %v\n", len(strSlice), cap(strSlice))
	fmt.Println(reflect.ValueOf(strSlice).Kind())
}

Output

intSlice Len: 10 Cap: 10
slice
strSlice Len: 10 Cap: 20
slice


Declare Slice from an Array

In Go You can create a slice by slicing an array:

Example


package main
import ("fmt")

func main() {
  arr1 := [6]int{1, 2, 3, 4, 5, 6}
  myslice := arr1[2:4]

  fmt.Printf("myslice = %v\n", myslice)
  fmt.Printf("length = %d\n", len(myslice))
  fmt.Printf("capacity = %d\n", cap(myslice))
}

Output

myslice = [2 3]
length = 2
capacity = 4


Declare Slice using []datatype{values} format


Example


package main
import ("fmt")

func main() {
  myslice := []string{"Jhon", "William", "Brandon", "Joshua"}
  fmt.Println(len(myslice))
  fmt.Println(cap(myslice))
  fmt.Println(myslice)
}

Output

4
4
[Jhon William Brandon Joshua]

Conclusion

In this page (written and validated by ) you learned about Go Slices . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go Add - Append - Change - Access - Copy Slices.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.