Latest Code Tutorials

Golang Slices Example | Slices in Go Tutorial


Golang Slices Example | Slices in Go Tutorial is today’s topic. An array has a fixed size. A 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 Slices 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.

Creating the slice from an 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 Slices Example | Slices in Go Tutorial is over.

Recommended Posts

Go Slice Append Example

Go For Loop Example

Go Custom Type Declarations Tutorial With Example

How To Install Go On Mac

Golang Receiver Function Tutorial With Example

Leave A Reply

Your email address will not be published.

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