Latest Code Tutorials

Golang Slice Example | Slices in Go Tutorial

Golang slice is a dynamically-sized, flexible view array. Go slice is a reference to a contiguous segment of an array. Go Slice is basically a pointer, with additional properties about the array. Let me clear one thing here, Golang Slice and Golang Array is totally different data type.

Golang array has a fixed size but Go slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array. A Slice is the segment of an array. The slices build on arrays and provide more power, flexibility, and convenience compared to the arrays.

Unlike the arrays, slices are typed only by the elements they contain.

Slices support several more that make them richer than arrays. One of the builtin functions is the Golang slice append(), which returns a slice containing one or more new values.

Golang Slice Example

If you do not know how to install Golang on mac the check out my How To Install Go On Mac tutorial.

Just like the arrays, Slices are indexable and have a length. But unlike arrays, they can be resized. In terms of programming paradigm, The Slice is just a reference to the underlying array.

Let’s see the syntax of Slices in Golang.

Declaring a Slice

You can declare a slice using the following code.

var variablename []data type

Let’s declare an actual slice.

var sl = []string

Now, let’s define a slice in go.

var sl = []string{"AppDividend", "Krunal"}

Okay, now let’s take an actual example and print the slice on the console.

/* hello.go */

package main

import "fmt"

func main() {
  var sl = []string{"AppDividend", "Krunal"}

  fmt.Println("Slice Example: ", sl)

So, the main function, we have defined the slice using two strings and then display the slice on the console. Run the file using the following command.

go run hello.go

The output is the following.

Go Slices Tutorial With Example

We can also initialize the slice like the following code.

/* hello.go */

package main

import "fmt"

func main() {
  sl := []string{

  fmt.Println("Slice Example: ", sl)

We will get the same output.

How to create a slice from array

Since the slice is the segment of an array, we can create the slice from an array.

Let’s take the following example.

/* hello.go */

package main

import "fmt"

func main() {
   var a = [5]string{"Javascript", "PHP", "Go", "Python", "Dart"}
   var b = a[1:3]
   fmt.Println("Slice Example: ", b)
   fmt.Println("Array Example: ", a)

Here, first, we have defined a fixed-length array called a.

Since the slice is formed by specifying two indices, a low and high bound, separated by a colon.


That means, from an array a, start the element from 0 and pick the values between 1(low) to 3(high). Which means we will form the slice whose index in an array is and 2. 

In the above example, PHP and Go have index 1 and 2. So the output slice will be of those elements.

Let’s see the output of Slice and Array in the console.

Creating a slice from an array

The low and high indices in a slice expression are optional. The default value for low is 0 and high is the length of a slice.

A slice is like an array which is a container to hold elements of the same data type, but slice can vary in size.

Finally, Golang Slice Example is over.

Recommended Posts

Golang slice append()

Golang for loop

Golang custom type declarations

Golang Receiver Function

How to install Golang on Mac

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.