Array Manipulation in Go

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This challenge will test your understanding of arrays in Golang. You will be required to create and manipulate arrays of different types.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/DataTypesandStructuresGroup(["`Data Types and Structures`"]) go/DataTypesandStructuresGroup -.-> go/arrays("`Arrays`") subgraph Lab Skills go/arrays -.-> lab-15373{{"`Array Manipulation in Go`"}} end

Arrays

You are required to create an array of integers with a length of 5. You will then set a value at a specific index and retrieve a value from a specific index. You will also be required to find the length of the array and declare and initialize an array in one line. Finally, you will create a two-dimensional array and initialize it with values.

Requirements

  • Create an array of integers with a length of 5
  • Set a value at a specific index and retrieve a value from a specific index
  • Find the length of the array
  • Declare and initialize an array in one line
  • Create a two-dimensional array and initialize it with values

Example

## Note that arrays appear in the form `[v1 v2 v3 ...]`
## when printed with `fmt.Println`.
$ go run arrays.go
emp: [0 0 0 0 0]
set: [0 0 0 0 100]
get: 100
len: 5
dcl: [1 2 3 4 5]
2d: [[0 1 2] [1 2 3]]

Summary

In this challenge, you learned how to create and manipulate arrays in Golang. You learned how to set and retrieve values from specific indices, find the length of an array, declare and initialize an array in one line, and create a two-dimensional array and initialize it with values.

Other Go Tutorials you may like