Learn

While empty arrays are useful, so are arrays already populated with elements! Sometimes we already know which values we are going to put in our array.

Imagine we are doing some math homework, and using the same triangles over an over again. We could declare two arrays with the values of the side lengths and angles for that triangle.

triangleSides := [3]int{15, 26, 30} triangleAngles := [...]int{30, 60, 90}

Notice how the syntax between these two lines is different. When creating an array with values, we can have the compiler determine the length automatically using ... ellipsis syntax.

So now we have some values, but how can we use these to do our math homework? In the next exercise, we will learn how to access the values stored in our arrays.

Before that, let’s create some arrays populated with values.

Instructions

1.

Imagine we needed to store the correct result for a 5 number lottery. The correct numbers for that lottery are:

5, 48, 32, 1, 6

Create an array with those numbers as its values.

2.

Print the array containing the lottery numbers into the output terminal.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?