Skip to Content
Introducing Go
book

Introducing Go

by Caleb Doxsey
January 2016
Beginner
170 pages
1h 59m
English
O'Reilly Media, Inc.
Content preview from Introducing Go

Chapter 5. Arrays, Slices, and Maps

In Chapter 2, we learned about Go’s basic types. In this chapter, we will look at three more built-in types: arrays, slices, and maps.

Arrays

An array is a numbered sequence of elements of a single type with a fixed length. In Go, they look like this:

var x [5]int

x is an example of an array that is composed of five ints. Try running the following program:

package main

import "fmt"

func main() {
    var x [5]int
    x[4] = 100
    fmt.Println(x)
}

You should see this:

[0 0 0 0 100]

x[4] = 100 should be read “set the fifth element of the array x to 100.” It might seem strange that x[4] represents the fifth element instead of the fourth, but like strings, arrays are indexed starting from 0. Arrays are accessed in a similar way. We could change fmt.Println(x) to fmt.Println(x[4]) and we would get 100.

Here’s an example program that uses arrays:

func main() {
    var x [5]float64
    x[0] = 98
    x[1] = 93
    x[2] = 77
    x[3] = 82
    x[4] = 83

    var total float64 = 0
    for i := 0; i < 5; i++ {
        total += x[i]
    }
    fmt.Println(total / 5)
}

This program computes the average of a series of test scores. If you run it, you should see 86.6. Let’s walk through the program:

  1. First, we create an array of length 5 to hold our test scores, then we fill up each element with a grade.
  2. Next, we set up a for loop to compute the total score.
  3. Finally, we divide the total score by the number of elements to find the average.

This program works, but Go provides some features we can use to improve it. Specifically, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Go in Action

Go in Action

Brian Ketelsen, Erik St. Martin, William Kennedy
Crucial Conversations

Crucial Conversations

Joseph Grenny, Kerry Patterson, Ron McMillan, Al Switzler, Emily Gregory
The Goal

The Goal

Eliyahu M. Goldratt, Jeff Cox
Storytelling with You

Storytelling with You

Cole Nussbaumer Knaflic

Publisher Resources

ISBN: 9781491941997Errata Page