Heim Backend-Entwicklung Golang Sichere Verwendung von Maps in Golang: Unterschiede in der Deklaration und Initialisierung

Sichere Verwendung von Maps in Golang: Unterschiede in der Deklaration und Initialisierung

Aug 31, 2024 pm 10:31 PM

Safely using Maps in Golang: Differences in declaration and initialization

Introduction

This week, I was working on one of the API wrapper packages for golang, and that dealt with sending post requests with URL encoded values, setting cookies, and all the fun stuff. However, while I was constructing the body, I was using url.Value type to construct the body, and use that to add and set key-value pairs. However, I was getting a wired nil pointer reference error in some of the parts, I thought it was because of some of the variables I set manually. However, by debugging closer, I found out a common pitfall or bad practice of just declaring a type but initializing it and that causing nil reference errors.

In this post, I will cover, what are maps, how to create maps, and especially how to properly declare and initialize them. Create a proper distinction between the declaration and initialization of maps or any similar data type in golang.

What is a map in Golang?

A map or a hashmap in golang is a basic data type that allows us to store key-value pairs. Under the hood, it is a header map-like data structure that holds buckets, which are basically pointers to bucket arrays (contiguous memory). It has hash codes that store the actual key-value pairs, and pointers to new buckets if the current overflows with the number of keys. This is a really smart data structure that provides almost constant time access.

How to create maps in Golang

To create a simple map in golang, you can take an example of a letter frequency counter using a map of string and integer. The map will store the letters as keys and their frequency as values.

package main

import "fmt"

func main() {
    words := "hello how are you"
    letters := map[string]int{}

    for _, word := range words {
        wordCount[word]++
    }

    fmt.Println("Word counts:")
    for word, count := range wordCount {
        fmt.Printf("%s: %d\n", word, count)
    }
}
Nach dem Login kopieren
$ go run main.go

Word counts:
e: 2
 : 3
w: 1
r: 1
y: 1
u: 1
h: 2
l: 2
o: 3
a: 1
Nach dem Login kopieren

So, by initializing the map as map[string]int{} you will get an empty map. This can be then used to populate the keys and values, we iterate over the string, and for each character (rune) we cast that byte of character into the string and increment the value, the zero value for int is 0, so by default if the key is not present, it will be zero, it is a bit of double-edged swords though, we never know the key is present in the map with the value 0 or the key is not present but the default value is 0. For that, you need to check if the key exists in the map or not.

For further details, you can check out my Golang Maps post in detail.

Difference between declaration and initialization

There is a difference in declaring and initializing any variable in a programming language and has to do a lot more with the implementation of the underlying type. In the case of primary data types like int, string, float, etc. there are default/zero values, so that is the same as the declaration and initialization of the variables. However, in the case of maps and slices, the declaration is just making sure the variable is available to the scope of the program, however for initialization is setting it to its default/zero value or the actual value that should be assigned.

So, declaration simply makes the variable available within the scope of the program. For maps and slices, declaring a variable without initialization sets it to nil, meaning it points to no allocated memory and cannot be used directly.

Whereas the initialization allocates memory and sets the variable to a usable state. For maps and slices, you need to explicitly initialize them using syntax like myMap = make(map[keyType]valueType) or slice = []type{}. Without this initialization, attempting to use the map or slice will lead to runtime errors, such as panics for accessing or modifying a nil map or slice.

Let's looks at the values of a map when it is declared/initialized/not initialized.

Imagine you're building a configuration manager that reads settings from a map. The map will be declared globally but initialized only when the configuration is loaded.

  1. Declared but not initialized

The below code demonstrates a map access that is not initialized.

package main

import (
    "fmt"
    "log"
)

// Global map to store configuration settings
var configSettings map[string]string // Declared but not initialized

func main() {
    // Attempt to get a configuration setting before initializing the map
    serverPort := getConfigSetting("server_port")
    fmt.Printf("Server port: %s\n", serverPort)
}

func getConfigSetting(key string) string {
    if configSettings == nil {
        log.Fatal("Configuration settings map is not initialized")
    }
    value, exists := configSettings[key]
    if !exists {
        return "Setting not found"
    }
    return value
}
Nach dem Login kopieren
$ go run main.go
Server port: Setting not found
Nach dem Login kopieren
  1. Declared and Initialized at the same time

The below code demonstrates a map access that is initialized at the same time.

package main

import (
    "fmt"
    "log"
)

// Global map to store configuration settings
var configSettings = map[string]string{
    "server_port":  "8080",
    "database_url": "localhost:5432",
}

func main() {
    serverPort := getConfigSetting("server_port")
    fmt.Printf("Server port: %s\n", serverPort)
}

func getConfigSetting(key string) string {
    value, exists := configSettings[key]
    if !exists {
        return "Setting not found"
    }
    return value
}
Nach dem Login kopieren
$ go run main.go
Server port: 8080
Nach dem Login kopieren
  1. Declared and later initialized

The below code demonstrates a map access that is initialized later.

package main

import (
    "fmt"
    "log"
)

// Global map to store configuration settings
var configSettings map[string]string // declared but not initialized

func main() {
    // Initialize configuration settings
    initializeConfigSettings()
    // if the function is not called, the map will be nil

    // Get a configuration setting safely
    serverPort := getConfigSetting("server_port")
    fmt.Printf("Server port: %s\n", serverPort)
}

func initializeConfigSettings() {
    if configSettings == nil {
        configSettings = make(map[string]string) // Properly initialize the map
        configSettings["server_port"] = "8080"
        configSettings["database_url"] = "localhost:5432"
        fmt.Println("Configuration settings initialized")
    }
}

func getConfigSetting(key string) string {
    if configSettings == nil {
        log.Fatal("Configuration settings map is not initialized")
    }
    value, exists := configSettings[key]
    if !exists {
        return "Setting not found"
    }
    return value
}
Nach dem Login kopieren
$ go run main.go
Configuration settings initialized
Server port: 8080
Nach dem Login kopieren

In the above code, we declared the global map configSettings but didn't initialize it at that point, until we wanted to access the map. We initialize the map in the main function, this main function could be other specific parts of the code, and the global variable configSettings a map from another part of the code, by initializing it in the required scope, we prevent it from causing nil pointer access errors. We only initialize the map if it is nil i.e. it has not been initialized elsewhere in the code. This prevents overriding the map/flushing out the config set from other parts of the scope.

Pitfalls in access of un-initialized maps

But since it deals with pointers, it comes with its own pitfalls like nil pointers access when the map is not initialized.

Let's take a look at an example, a real case where this might happen.

package main

import (
    "fmt"
    "net/url"
)

func main() {
        var vals url.Values
        vals.Add("foo", "bar")
        fmt.Println(vals)
}
Nach dem Login kopieren

This will result in a runtime panic.

$ go run main.go
panic: assignment to entry in nil map

goroutine 1 [running]:
net/url.Values.Add(...)
        /usr/local/go/src/net/url/url.go:902
main.main()
        /home/meet/code/playground/go/main.go:10 +0x2d
exit status 2
Nach dem Login kopieren

This is because the url.Values is a map of string and a list of string values. Since the underlying type is a map for Values, and in the example, we only have declared the variable vals with the type url.Values, it will point to a nil reference, hence the message on adding the value to the type. So, it is a good practice to use make while declaring or initializing a map data type. If you are not sure the underlying type is map then you could use Type{} to initialize an empty value of that type.

package main

import (
    "fmt"
    "net/url"
)

func main() {
        vals := make(url.Values)
        // OR
        // vals := url.Values{}
        vals.Add("foo", "bar")
        fmt.Println(vals)
}
Nach dem Login kopieren
$ go run urlvals.go
map[foo:[bar]]
foo=bar
Nach dem Login kopieren

It is also recommended by the golang team to use the make function while initializing a map. So, either use make for maps, slices, and channels, or initialize the empty value variable with Type{}. Both of them work similarly, but the latter is more generally applicable to structs as well.

Conclusion

Understanding the difference between declaring and initializing maps in Golang is essential for any developer, not just in golang, but in general. As we've explored, simply declaring a map variable without initializing it can lead to runtime errors, such as panics when attempting to access or modify a nil map. Initializing a map ensures that it is properly allocated in memory and ready for use, thereby avoiding these pitfalls.

By following best practices—such as using the make function or initializing with Type{}—you can prevent common issues related to uninitialized maps. Always ensure that maps and slices are explicitly initialized before use to safeguard against unexpected nil pointer dereferences

Thank you for reading this post, If you have any questions, feedback, and suggestions, feel free to drop them in the comments.

Happy Coding :)

Das obige ist der detaillierte Inhalt vonSichere Verwendung von Maps in Golang: Unterschiede in der Deklaration und Initialisierung. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Crossplay haben?
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Was sind die Schwachstellen von Debian Openensl Was sind die Schwachstellen von Debian Openensl Apr 02, 2025 am 07:30 AM

OpenSSL bietet als Open -Source -Bibliothek, die in der sicheren Kommunikation weit verbreitet sind, Verschlüsselungsalgorithmen, Tasten und Zertifikatverwaltungsfunktionen. In seiner historischen Version sind jedoch einige Sicherheitslücken bekannt, von denen einige äußerst schädlich sind. Dieser Artikel konzentriert sich auf gemeinsame Schwachstellen und Antwortmaßnahmen für OpenSSL in Debian -Systemen. DebianopensL Bekannte Schwachstellen: OpenSSL hat mehrere schwerwiegende Schwachstellen erlebt, wie z. Ein Angreifer kann diese Sicherheitsanfälligkeit für nicht autorisierte Lesen sensibler Informationen auf dem Server verwenden, einschließlich Verschlüsselungsschlüssel usw.

Wie verwenden Sie das PPROF -Tool, um die Go -Leistung zu analysieren? Wie verwenden Sie das PPROF -Tool, um die Go -Leistung zu analysieren? Mar 21, 2025 pm 06:37 PM

In dem Artikel wird erläutert, wie das PPROF -Tool zur Analyse der GO -Leistung verwendet wird, einschließlich der Aktivierung des Profils, des Sammelns von Daten und der Identifizierung gängiger Engpässe wie CPU- und Speicherprobleme.Character Count: 159

Wie schreibt man Unit -Tests in Go? Wie schreibt man Unit -Tests in Go? Mar 21, 2025 pm 06:34 PM

In dem Artikel werden Schreiben von Unit -Tests in GO erörtert, die Best Practices, Spottechniken und Tools für ein effizientes Testmanagement abdecken.

Welche Bibliotheken werden für die Operationen der schwimmenden Punktzahl in Go verwendet? Welche Bibliotheken werden für die Operationen der schwimmenden Punktzahl in Go verwendet? Apr 02, 2025 pm 02:06 PM

In der Bibliothek, die für den Betrieb der Schwimmpunktnummer in der GO-Sprache verwendet wird, wird die Genauigkeit sichergestellt, wie die Genauigkeit ...

Was ist das Problem mit Warteschlangen -Thread in Go's Crawler Colly? Was ist das Problem mit Warteschlangen -Thread in Go's Crawler Colly? Apr 02, 2025 pm 02:09 PM

Das Problem der Warteschlange Threading In Go Crawler Colly untersucht das Problem der Verwendung der Colly Crawler Library in Go -Sprache. Entwickler stoßen häufig auf Probleme mit Threads und Anfordern von Warteschlangen. � ...

Was ist der Befehl go fmt und warum ist es wichtig? Was ist der Befehl go fmt und warum ist es wichtig? Mar 20, 2025 pm 04:21 PM

In dem Artikel wird der Befehl go fMT in Go -Programmierung erörtert, in dem Code formatiert werden, um offizielle Richtlinien für den Stil einzuhalten. Es zeigt die Bedeutung von GO FMT für die Aufrechterhaltung der Debatten mit Codekonsistenz, Lesbarkeit und Reduzierung von Stildebatten. Best Practices fo

Ist es vielversprechender, Java oder Golang von Front-End zu Back-End-Entwicklung zu verwandeln? Ist es vielversprechender, Java oder Golang von Front-End zu Back-End-Entwicklung zu verwandeln? Apr 02, 2025 am 09:12 AM

Backend Learning Path: Die Erkundungsreise von Front-End zu Back-End als Back-End-Anfänger, der sich von der Front-End-Entwicklung verwandelt, Sie haben bereits die Grundlage von Nodejs, ...

PostgreSQL -Überwachungsmethode unter Debian PostgreSQL -Überwachungsmethode unter Debian Apr 02, 2025 am 07:27 AM

In diesem Artikel werden eine Vielzahl von Methoden und Tools eingeführt, um PostgreSQL -Datenbanken im Debian -System zu überwachen, um die Datenbankleistung vollständig zu erfassen. 1. verwenden Sie PostgreSQL, um die Überwachungsansicht zu erstellen. PostgreSQL selbst bietet mehrere Ansichten für die Überwachung von Datenbankaktivitäten: PG_STAT_ACTIVITY: Zeigt Datenbankaktivitäten in Echtzeit an, einschließlich Verbindungen, Abfragen, Transaktionen und anderen Informationen. PG_STAT_REPLIKATION: Monitore Replikationsstatus, insbesondere für Stream -Replikationscluster. PG_STAT_DATABASE: Bietet Datenbankstatistiken wie Datenbankgröße, Transaktionsausschüsse/Rollback -Zeiten und andere Schlüsselindikatoren. 2. Verwenden Sie das Log -Analyse -Tool PGBADG

See all articles