Arrays in Golang

A Beginners guide to arrays in golang.

What is an array?

An array is a collection of elements of a specified length and type.

  • Quick Note: Most go programmers use slices instead of arrays when coding in golang.

Key Points:

There are two ways of declaring an array in golang:

  1. Declare and initialize : DaysOfTheWeek := [7]string{“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}
  2. Declare without giving the initial values: var TestScores [5]int (values initially default to 0 for integers)

Getting a value:

DaysOfTheWeek[0] // “Sunday”

Setting a value:

TestScores[0] = 100 // sets index 0 to the value of 100

Get the length of an Array:

len(DayOfTheWeek) //7

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Gyulnara Grigoryan
Gyulnara Grigoryan

Written by Gyulnara Grigoryan

Software Engineer | Always Learning | interests: coding, photography, piano, writing, reading, and dancing

No responses yet

Write a response