TagArrays

Arrays and Slices in Go

We’ve looked at strings, numbers, and boolean values in Go. Now, we can take a look at more complex types in Go. The two main types that we are going to look at are Arrays and Slices in Go. Slices are used more in Go than arrays but slices are an abstraction on top of arrays in Go.

Arrays in Go

In Go, an array is a sequence of elements with a specific length. Both the type of the elements and the length are part of an array’s type information in Go. If we make a default array of ints of length five; the array will contain five zeros. Arrays are mutable in Go but only the elements can be changed. The length of an array in Go is fixed after declaration. We can change these elements using the index for the array.

In the examples above, you can see that we can define arrays, change their values, and can use the short declaration operator. Note that the index of an array starts at 0 and ends at the length minus one. An array of length five doesn’t have an element at index five. We also can’t use a negative index in an array in Go. 

go_array

Slices in Go

Having fixed sized arrays might seem a little limiting but this is why we have slices in Go. A slice in Go does not include the length in its type information. This means that the slice is actually a dynamically sized piece of data. This is part of why slices are used much more in Go programs. A slice literal is the same as an array literal without the length information.

In the example above, we create an array then use two slices to reference to it. When we change one of the values of one of the slices it also changes that value in the array and the other slice. This happens because slices are references to arrays. When you create a slice in Go, you are creating an array in the memory and referencing it. The slice itself does not hold any data, it just describes an underlying array. This is true even when you create and manipulate a slice without first creating an array. 

go_slicesBecause slices are references to arrays, they also have a capacity. The capacity of a slice is the length of the underlying array. Other slices can also reference the same underlying array like in the example above. You can use the len() and the cap() functions to find the length and capacity of a slice. You can also add values to a slice using a function append(). When you append a slice above its capacity, a new array will be created in memory for the slice to reference. We can also use the make keyword to create slices.

In our example above, we use the make keyword to create two slices. We then create an array and point to it with a third slice. After that, we append three more values to this slice. We then change one of the original indexes to prove that it is no longer pointing at the original array. If you want a more technical explanation about slices then look at this google blog article: Go Slices.

Multidimensional Data

We can also make multidimensional arrays and slices in Go. A multidimensional Array or Slice is an Array of Arrays or a Slice of Slices.  You can denote these multidimensional structures using multiple brackets. Here are a few quick examples.

In our first example, we just define a two-dimensional array of zeroes.  In our second example, we build a two-dimensional slice by creating and appending two slices to our original slice.  Multidimensional arrays and slices can be extremely useful in Go. One easy way to create multidimensional arrays or slices is to use nested for loops. 

Conclusion

In this post, we talked about arrays and slices in Go. We talked about how slices are references for arrays and how arrays have a fixed length in Go. We also talked about how Go manages its memory for arrays and slices. In the end, we looked at multidimensional arrays and slices in Go. In our next few posts, we will look at maps, embedded maps and then functions in Go.

JavaScript Object Notation

In the last post, we took a look at a Todo application. Our Todo app was very simple and it has no means to store data. To be able to store data we need to learn to use JSON. In this post, we are going to talk about the JavaScript Object Notation or JSON for short.

What is JSON? JSON or the JavaScript Object Notation is a way for us to store information in an organized and easy to access manner. Quite simply, it gives us a method of storing data in a readable format that is also very logical as well. From the name, JavaScript Object Notation, we can infer that this method relies heavily on JavaScript Objects. Here is a simple example in JSON format.

We’ve created an object here that we can access with the variable person. We’ve used the object literal notation to define the variable as an object. Inside of this object, we can have any number of "name" : "value” pairs separated by commas. When we want to access the date inside of this object we can simply refer to the name of the property we need using the standard “dot syntax”.

Nested Arrays and Objects

We can also store more objects into one single variable by taking advantage of arrays. For example, if we want to have two Person objects in one variable expression we can simply enclose them in square brackets and separate the two objects with a comma. Another way to store multiple people in one variable is through the use of nested object notation. Here is an example of both.

Using nested objects to store data makes use of much easier to understand syntax than using arrays. As you can see, we just simply use the dot syntax and call the outer object name, the inner object name then the property name that we wish to access.

The great thing about JSON or the JavaScript Object Notation is that it is lightweight and also is very readable. It also makes use of the JavaScript language which we are currently working with. We don’t have to import SQL or another database language to give our applications a backend. While this is true, it is worth noting that JSON isn’t all technically JavaScript. JSON just happens to be syntactically identical to the code for creating JavaScript Objects.

Language Independence

JavaScript Object NotationJSON the JavaScript Object Notation typically is defined in its own file, a .json file. If you build a node application, you will use JSON to store your dependencies and project information. Also, because of it’s easy of use, JSON is used as a language independent way to store data for applications. This means, that you will often see JSON in Ruby, Python, Perl, and PhP applications to name a few examples. The standard boilerplate node package.json file looks something like this.

Closing

Today, we took a brief look at JSON or the JavaScript Object Notation. We talked about how to create JSON, what it is used for and why it is useful. In our up and coming tutorials, we will look at some more advanced JavaScript techniques through the use of examples.

Arrays in JavaScript

In our last post, we talked about the For and While loops.  Before we can continue with loops, we need to take a look at arrays in JavaScript. Arrays in JavaScript are a special type of variable that will let you store multiple values. Arrays are not a basic data structure in JavaScript, instead, they are considered objects. Also, an array in JavaScript can hold many different types inside of it and the typeof operator will return the object as the type for an array.

There are a few ways of creating an array but the simplest is just to use the literal notation:

You can also change the way the array looks through the use of whitespace, line breaks, and spaces:

The other common way of creating an array is to use the new keyword to instantiate an array the same way you would an object. (more on objects and instantiation later)

Let us look at an example array so that we can see how we can use these complex data structures in JavaScript. Say we wanted to have a list of programming languages in our program for whatever reason, we could hold the names inside of an array of strings:

As you can see, our programLang variable contains a bunch of strings which have various names of programming languages inside of them. We can access each of the elements of this array through the use of the index of the array.  This is done through a notation that looks like this:

Also, we can use the length function to quickly find out how many pieces of data are inside of the array as shown in the example above. Our programLang array has 7 different items in it.  Notice, however, that the index of the array will start at 0 and not 1. This is a very common occurrence in programming languages and JavaScript is no exception to this rule. The first element of programLang is "Java" but calling programLang[1] returns "JavaScript". To get the first element of this array we must call it like this: programLang[0].

Looping through an Array

If we want to display the entire array in a code block we can easily loop through the array using its index. Here is an example using the programLang array (you can copy and paste it into your console to see for yourself):

arrayWe work around the index problem by starting our iterator at 0 and using less-than < rather than less-than-or-equal-to <= as our conditional. As the variable i increments up from 0 to 6, we get each of the elements of this array printed to the console with the words "The language is " concatenated before it. Also, note that this for loop structure will work with any array, no matter how large or small, though there is a better notation.

Using a for loop over an array or an object is so common that a special syntax was introduced to make things quicker. This syntax is called a for/in loop. The for/in loop actually bypasses the creation of an iterator in the standard way. Look at the example below.

As you can see, we don’t have to use a conditional. The in keyword works like a shorthand and automatically iterates the iterator i over the length of the array or object that we pass it which in this case is our programLang array.

Functions and Arrays

Arrays have various inbuilt functions like the length function. Here are a few examples of the different types of functions that come with arrays:

You should note that arrays are mutable. Calling sort() on an array will change the order of the array permanently. So will changing a value of the array by index, for example, programLang[1] = true; will change the second element of our array to the boolean value of true instead of "JavaScript".  For anyone who already knows another language, keep in mind that JavaScript arrays do not support hashes or non-numbered indexes. If you attempt to use a non-numerical index in an array it will be converted to an object.

Conclusion

In this post, we looked at arrays in JavaScript and the basic properties and inbuilt functions of arrays. We also looked at the for/in loop structure for both arrays and Objects. In our next posts, we will start to talk about functions and Objects in JavaScript in a more broad manner and we will finish our talk about loops.