AppDividend
Latest Code Tutorials

Go Slices Tutorial With Example | Slices in Golang

0

Go Slices Tutorial With Example | Slices in Golang 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 is the builtin append, which returns a slice containing one or more new values.

Go Slices Tutorial With 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 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{
	"AppDividend",
	"Krunal"}

  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.

a[1:3]

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 same data type, but slice can vary in size.

Finally, Go Slices Tutorial With Example | Slices in Golang is over.

Leave A Reply

Your email address will not be published.

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