JIYIK CN >

Current Location:Home > Learning > PROGRAM > Go >

Declaring constant Map in Go

Author:JIYIK Last Updated:2025/04/15 Views:

Map is a collection of key-value pairs which can be sorted in any order. It assigns values ​​to keys.

The keys in a Map are always unique, but the values ​​are not always unique. The map data structure is used for fast key-based data lookup, retrieval, and deletion.

It is a common data structure in computer science. In this article, we will look at declaring constant maps in Go.


Declaring constant maps in Go using map[int]string

This example will use map[int]stringto declare a constant map in Go.

example:

package main

import (
    "fmt"
)

var constMap = map[int]string{
    1: "Jay",
    2: "Adam",
    3: "Mike",
    4: "Wiz",
    5: "Lucas",
}

func main() {
    fmt.Println(constMap[1])
    fmt.Println(constMap[5])
}

Output:

Jay
Lucas

Declaring constant maps in Go using map[string]int

This example will use map[string]intto declare a constant map in Go.

example:

package main

import (
    "fmt"
)

var constMap = map[string]int{
    "Jay":   1,
    "Adam":  2,
    "Mike":  3,
    "Wiz":   4,
    "Lucas": 5,
}

func main() {
    fmt.Println(constMap["Jay"])
    fmt.Println(constMap["Lucas"])
}

Output:

1
5

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Install GoLang using Brew

Publish Date:2025/04/15 Views:82 Category:Go

This article describes how to install GoLang using Brew on Linux or macOS. Install GoLang using Brew brew installs missing packages in Linux and macOS. It makes it easy to install GoLang on Linux or macOS. Follow the steps below to install

GoLang RWMutex Detailed Introduction

Publish Date:2025/04/15 Views:116 Category:Go

This article introduces how to use rwmutex in Go language. Go language RWMutex mutex is the abbreviation of mutual exclusion, which is used to keep track of which thread has accessed a variable at any time. Mutex is a data structure provide

Enabling CORS in GoLang

Publish Date:2025/04/15 Views:102 Category:Go

This article describes how to enable and use CORS in GoLang. Go language CORS Cross-origin resource sharing (CORS) is a process based on HTTP headers that defines the origins from which browsers are allowed to load and use resources. CORS i

Function pointers in Go

Publish Date:2025/04/15 Views:83 Category:Go

A pointer in Go is a special-purpose variable that stores the memory address of other variables and the point where the memory is located. It can also access the value stored in that memory location. Pointer declaration in Go var pointer_na

Duration in Go

Publish Date:2025/04/15 Views:89 Category:Go

Go duration conversion can be done in a variety of ways. The time library and the time.duration method are often used to calculate and display time. Note that duration refers to the time elapsed between two defined time objects, as an int64

Create a new directory in Go

Publish Date:2025/04/15 Views:112 Category:Go

Go has many ways to create directories, one of which is os.Mkdir . We can assign specified names and permission bits to the newly formed directory. os.MkdirAll Useful for recursively creating multiple directories, including any missing pare

How to read a file into a string in GoLang

Publish Date:2025/04/15 Views:90 Category:Go

The Go language provides a lot of file manipulation tools, one of which is how to read files into strings. ioutil.ReadFile() , File.Read() , buf.ReadFrom() and strings.Builder are just a few of the methods that can be used to efficiently wr

Getting a string representation of a structure in Go

Publish Date:2025/04/15 Views:63 Category:Go

Go allows us to serialize data from structures using a variety of simple standard methods. Converting a structure to a string using String method in Go The GoLang package String helps implement simple functions to manipulate and edit UTF-8

Convert JSON to struct in Go

Publish Date:2025/04/15 Views:126 Category:Go

This article describes how to convert JSON to struct in GoLang . Convert JSON to Struct using Unmarshal method in Go The encoding/json package of the Go language provides a function Unmarshal to convert JSON data into byte format. This func

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial