Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Map Declaration and Initialization | Intro to Structs & Maps
Introduction to GoLang

bookMap Declaration and Initialization

We can declare an empty map using the make() function. Below is the syntax we need to follow:

var mapName = make(map[keyType]valueType)
// or
mapName := make(map[keyType]valueType)

Here, mapName is the name of the map, keyType is the expected type of the keys, and valueType is the expected type of the values. The term map is a keyword.

We can declare a map called courseData with keys of type string and values of type int.

index.go

index.go

copy
1
var courseData = make(map[string]int)

If we attempt to output this map, we will receive an output indicating an empty map:

index.

index.

copy
1
fmt.Println(courseData) // Output: map[]

We can initialize a map with some data using the following syntax:

var mapName = map[keyType]valueType = {
    key1: value1,    
    key2: value2,
    key3: value 3,
    …
}

Note

You can place or omit spaces in the map declaration syntax; therefore, both map[keyType]valueType and map [keyType] valueType are valid.

Using the above syntax, we can create a map that has some initial data in it:

index.go

index.go

copy
1234567
myMap := map[string]int { "a": 10, "b": 11, "c": 12, } fmt.Println(myMap) // Output: map[a:10 b:11 c:12]
question mark

Which syntax is correct for creating a new empty map?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 5

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 1.96

bookMap Declaration and Initialization

Sveip for å vise menyen

We can declare an empty map using the make() function. Below is the syntax we need to follow:

var mapName = make(map[keyType]valueType)
// or
mapName := make(map[keyType]valueType)

Here, mapName is the name of the map, keyType is the expected type of the keys, and valueType is the expected type of the values. The term map is a keyword.

We can declare a map called courseData with keys of type string and values of type int.

index.go

index.go

copy
1
var courseData = make(map[string]int)

If we attempt to output this map, we will receive an output indicating an empty map:

index.

index.

copy
1
fmt.Println(courseData) // Output: map[]

We can initialize a map with some data using the following syntax:

var mapName = map[keyType]valueType = {
    key1: value1,    
    key2: value2,
    key3: value 3,
    …
}

Note

You can place or omit spaces in the map declaration syntax; therefore, both map[keyType]valueType and map [keyType] valueType are valid.

Using the above syntax, we can create a map that has some initial data in it:

index.go

index.go

copy
1234567
myMap := map[string]int { "a": 10, "b": 11, "c": 12, } fmt.Println(myMap) // Output: map[a:10 b:11 c:12]
question mark

Which syntax is correct for creating a new empty map?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 5
some-alt