Questions tagged [go-map]

Go provides a built-in map type that implements a hash table. It is an unordered group of elements of one type, called the element type, indexed by a set of unique keys of another type, called the key type. The value of an uninitialized map is nil. A nil map behaves like an empty map when reading, but attempts to write to a nil map will cause a runtime panic; don't do that.

60 questions
1
vote
1 answer

Golang gives different result everytime using map for AOC 2021 Day 6 problem

I've been trying to solve Advent of Code 2021 and in day 6, I am trying this solution but the result is different everytime. What seems to be the problem? Is there any memory leakage with map? The input file can be found here The details of the…
Kanuor
  • 55
  • 5
1
vote
0 answers

Get embedded object values in an ordered map from MongoDb Go driver

GO version: 1.18.3 Mongodb version: 4.4 Mongodb driver used: go.mongodb.org/mongo-driver/bson I want to receive an embedded object saved in the database in the form of a map with preserved order (order matters for me). For this I have used following…
Amandeep kaur
  • 985
  • 3
  • 15
  • 35
1
vote
2 answers

A design question about Go's map and slice troubled me a lot

Why does Go's slice has 'replication trap', but map does not? Suppose we have a function that takes slice as an input parameter, and if slice is expanded in the function, only the copied slice structure is changed, instead of the original slice…
qizong007
  • 21
  • 1
1
vote
1 answer

Encapsulating "concurrency safety" in Go Maps

I have a struct, MyStruct, which contains a map. I want to make the access to the map safe for concurrent read and write but I also want to stick to the base Map and not use sync.Map. For this reason I create on MyStruct methods for insert, delete…
Picci
  • 16,775
  • 13
  • 70
  • 113
1
vote
0 answers

Are there any programming pitfalls of using a map with an empty interface as the KEY

Are there any programming pitfalls of using maps in this manner: type Set struct { theMap map[interface{}]struct{} } StringSet := NewSet("abc", "pqr") IntSet := NewSet(1, 2) DateSet := NewSet(time.Date(2021, 2, 15, 0, 0, 0, 0, time.UTC)) Just…
user15558657
1
vote
1 answer

Using msgp with interfaces and maps in Go

I have a map that uses an interface as the key. The map is defined like this MyMap map[Signature]Packets. The interface is Signature, and there will be two structs A and B that implement this interface. I am also using msgp to serialize these two…
Sebastian
  • 11
  • 2
1
vote
1 answer

How to pass a map as value?

I've a few Actors in my Golang App which require two maps to do their work. Those maps are generated by some intensive database transactions so I don't want to do that in every actor, as a result I've separated the maps generation from the Actors.…
Martin
  • 1,141
  • 10
  • 23
1
vote
4 answers

Slice out of bounds using [][]int but works with map[int][]int

Why does this code work graph := make(map[int][]int, 0) graph[0] = append(graph[0], 1) But if you replace first line with graph := make([][]int, 0) I get panic: runtime error: index out of range? It's very weird.
maxflow
  • 869
  • 4
  • 10
  • 16
0
votes
0 answers

Appending to slice in map which is type of map[any]any

I want to append to slice in map which is defined as map[any]any I am having type conversion issues and don't know how to treat result[index][ABSPath] as slice so I can append data to it Here is the example code: package main import ( "fmt" …
John Doe
  • 17
  • 1
0
votes
1 answer

Iterate over map of `interface{}` and call the same method on each item in Golang

I am working on a simple Console game to learn Go and got stuck on a seemingly simple issue that would be no problem in other languages, but seems almost impossible in Go. I have a map of interfaces as a field in struct like this: type Room struct…
matronator
  • 465
  • 6
  • 13
0
votes
0 answers

Slice to Map of pointers duplicates values

This is a beginner question about why I get such a result when trying to obtain a Map from a Slice in go lang. I have this code: func (p *ProductsStruct) ToMap() map[int32]*Product { result := map[int32]*Product{} for _, prod := range…
0
votes
1 answer

how to normalize (1:N) a csv file to a map in Go?

I'm trying to normalize a structure from a CSV file, which is like this: name, note 'Joe', 5 'Joe', 3 'Ashley', 1 'Ashley', 7 'Ashley', 4 to a map, that after read that file, will be reduced to: map [string][]string{ "joe" = {5, 3}, …
0
votes
1 answer

Trying to run a query in BQ passing labels in GoLang, but getting panic error

I am trying to run the following code to query in BQ passing labels in GoLang, but getting panic error (You may refer attached image for linenumbers). I am new to Golang, can someone please guide here? query := client.Query(`select * from…
newbie
  • 5
  • 2
0
votes
1 answer

What determines the order in which the results are generated from "for range" iteration of golang map?

I try to firgure out that what determines the order of results generated from "for range" iteration of golang map. I found that it is neither determined by the order of keys nor by the order of pushing. which is really weired. I want to figure this…
Tsiao Wang
  • 51
  • 7
0
votes
1 answer

Go: cannot call pointer method getFirstName on "Struct"

I have a simple struct and receiver. I try to set the map with the struct and then call the receiver. Like that: package main import ( "fmt" ) type myStruct struct { FirstName string LastName string } func (m *myStruct)…
user63898
  • 29,839
  • 85
  • 272
  • 514