64 questions
2
votes
2
answers
146
views
Golang map equivalent to postgres tstzrange and contains operator?
I'm looking for a way to cache data using a map (or equivalent) but instead of a comparable key (like int, string, etc) it would be lower and upper time boundaries. I would then be able to perform a ...
1
vote
1
answer
107
views
Trying to add a click listener to GO Maps Marker in WP GO MAPS Pro plugins
I'm using WP Go Maps Pro plugin. I'm trying to add a click event listener to a google map marker.
I get all the markers infos, but for some reason it won't add the listener.
Any idea what I'm doing ...
0
votes
0
answers
161
views
is there a way to get the original order of map[string]interface{}
I have a use case where the input payload consumed by my REST endpoint should flush it in the same order.
There is no specific pattern to the input, so I'm saving it as map[string]interface{} and that ...
0
votes
0
answers
360
views
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 ...
0
votes
1
answer
87
views
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 ...
2
votes
0
answers
1k
views
Why doesn’t 'dive' work on gin? (about validator)
I just would like to do like the below about validation on Gin(Golang).
type Accounts struct {
Accounts []*Account `json:"accounts" binding:"required,dive"`
}
type Account ...
1
vote
1
answer
53
views
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 ...
-1
votes
1
answer
282
views
Transform a golang map into another structure of map
I need help for transforming this input map into the output map. I try with switch/case and for but I didn't succeed it. Thanks a lot !
Input :
Values{
"toto_voiture_brand&...
0
votes
0
answers
99
views
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[...
0
votes
1
answer
155
views
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 [...
3
votes
1
answer
115
views
add colly package output text to map in golang
i was making a web scraper with colly package, where it collects the ContestName and ContestTime from a website and make a json file.
so i did like this
Contests := make(map[string]map[string]map[...
0
votes
1
answer
124
views
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?...
-3
votes
1
answer
157
views
How to acheive the order of insetion in nested maps using golang [duplicate]
In my project we are using nested map
map[string]map[string]map[string]string
for insertion of the data into cache.
While preparing the data the db data has to be compared with the cache data(nested ...
1
vote
1
answer
678
views
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 ...
0
votes
1
answer
2k
views
Go: cannot call pointer method getFirstName on "Struct" [duplicate]
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 ...
1
vote
0
answers
525
views
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 (...
-1
votes
1
answer
147
views
Values in map changing after refresh
Im trying to create a map that remembers its values when the page is recalled. I declared it outside the function so it would remain the same but the map still initializes to the defauly=t values when ...
3
votes
1
answer
12k
views
Golang map with any key type and any value type
can i create in golang a map with any key type and any value type ? , something like :
dict1 := map[interface]interface{}
Thanks a lot !
1
vote
2
answers
957
views
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 ...
-1
votes
1
answer
701
views
How to ensure that a Struct that is made up of channels and map gets passed by reference?
I have the following struct that contains channels and a map for storage of data. I want to be able to pass that struct into functions in order to make use of those channels so that once they are ...
-3
votes
1
answer
579
views
Unable to parse Golang map and nested json
I have below snippet
func fakeGetInclusterConfig() (*corev1.ConfigMap, error) {
configMap := &corev1.ConfigMap{
Data: map[string]map[string]string{"cluster-config.json&...
0
votes
1
answer
6k
views
how to convert map[string]interface{} data to struct
I do not know how to ask, so i will ask with an example.
I have some data like that
{
..
"velocityStatEntries": {
"8753": {
"estimated": {"value&...
6
votes
1
answer
2k
views
Check if key exists in map storing large values
To know a key k exist in a map M1[k]v is very straightforward in Go.
if v, ok := M1[k]; ok {
// key exist
}
'v': a value of a non-pointer type.
If v is large, To just check if a particular key ...
-1
votes
1
answer
332
views
Golang - Generate map with one to many relation
I am fairly new to golang and I am struggling to generate a one to many relationship map from existing map.
Here is my script playground
Explanation:-
I am trying to achieve the relation of each ...
0
votes
1
answer
6k
views
using maps with any value type as function parameter? [duplicate]
I am trying to create a function which takes in a map as a parameter, where the map uses string keys but the values can be ANY type.
How do I make this work in go? I tried using map[string]interface{} ...
-2
votes
1
answer
978
views
Best practice of counter in map struct golang
I have a code that is not clean. I want to increment Counter in struct of NameLike but I think this is not effective.
package main
import "fmt"
type NameLike struct {
Name string
...
3
votes
1
answer
2k
views
Rate limiter with gorilla mux
I am trying to implement http request limiter to allow 10 request per second per user by their usernames.
At the max 10 request can be hit to the server including requests which are under processing.
...
-2
votes
3
answers
2k
views
Golang: Appending keys from a map to a slice of slices
I ran into this simple Golang code and was surprised by Go's behavior here. Can someone explain what is going on here, and how to write the below code correctly?
As you can see, I have a map, where ...
6
votes
3
answers
4k
views
Check if a map is subset of another map
This question is already answered in many other languages. In golang with simple maps (no nesting) how to find out if a map is subset of another. for example: map[string]string{"a": "b&...
1
vote
1
answer
422
views
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 ...
1
vote
0
answers
84
views
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)
...
-2
votes
1
answer
869
views
Is deletion from slice of strings is faster than map of string with dummy values considering number of elements will be less than 500 [closed]
I will be receiving strings one by one from a framework, I need to hold them in some container and delete some of them later. Now I have 2 options :-
Create a slice of strings and then delete some ...
1
vote
1
answer
624
views
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 ...
0
votes
1
answer
61
views
Building a multi dimentional array reponse
I am trying to create a response like this:
"GameController" => [
"methods" => [
"get",
"post",
"put",
"...
0
votes
0
answers
575
views
How to reduce the memory footprint of this go program?
I'm trying to reduce the memory footprint of the code below, which is only about initializing the data I need to then perform a certain set of operations.
Pre-allocations are needed.
func bToMb(b ...
-3
votes
1
answer
116
views
Map concurrent usage [closed]
I came across this piece of code and was wondering if this needs to have a R/W Mutex.
method(){
var (
wg sync.WaitGroup
rwm sync.RWMutex
vcnRegionMap map[string][]core....
-1
votes
2
answers
2k
views
golang map not adding element
There is battleFoundmap in my code and i tried the add a element like this:(battle is not nil)
battleFoundMap[battle.ID] = battle.Answers
But when i debug it it returns 1:27: expected '==', found '=' ...
0
votes
3
answers
5k
views
Golang iterate over map of interfaces
I am trying to iterate over a map of interfaces in golang, it has the below structure, I am able to use for loop to iterate to a single level but couldn't go deep to get values of the interface.
Yaml
...
-1
votes
1
answer
759
views
How do I use RWMutex when concurrently modifying a map while iterating over it
I want to use a map's keys to request something from an API and then update the corresponding values based on the API's response.
My guess would be the following code.
Or maybe scratch this approach, ...
1
vote
1
answer
3k
views
What is the best way to create an array and loop in template file
I am using Gin gonic for my Go project, and in my footer.tmpl, I will have more than 10++ navigation links, rather than write 'link' multiple times, it would be much easier if I create an array ...
-2
votes
2
answers
1k
views
How to return key's value of a map of type empty interface
I have taken a variable like var u = make(map[string]interface{}) which means that a key could hold a string/int or another map.
When I do the following it gives error cannot use v (type interface {})...
0
votes
1
answer
648
views
How can I make a map of parent structs in go?
I'm coming from Java/Kotlin so I'm a bit new to the composition-over-inheritance world that Go is in :)
So in Java, I could make a
abstract class Pet
and a subclass
class Dog extends Pet
class ...
-3
votes
1
answer
618
views
How to append items to a map[string][]Struct
I am trying to append items to this struct I have:
type AuditSource struct {
Source map[string][]Pgm `json:"Source"`
}
type Pgm struct {
ID uint `json:"id,omitempty"`
...
3
votes
1
answer
2k
views
How to use GoMap in Cgo?
I'm trying to call Go from c++. My code operates on maps, and I can't seem to make maps work with cgo.
main.go:
package main
import (
"C"
"fmt"
)
func main() {}
//export PrintMap
func ...
1
vote
3
answers
3k
views
In Go is there a way to convert map of structure to slice of structure
I have to convert map of structure to slice of structure in Golang, i.e. source to target structure specified below.
// Source
var source map[string]Category
type Category struct {
A ...
0
votes
1
answer
200
views
How to inject data obtained from result(var) to result1(var) in golang
terraform.tfstate is the input file and packageservicelist.tf.json is the output file.
Both are converted into Map Files
The data which I traversed is stored in result & I want to copy the data ...
2
votes
1
answer
1k
views
How to know if 2 go maps reference the same data [duplicate]
Go maps are references to internal data. Meaning that when a map is "copied", they end up sharing the same reference and thus editing the same data. This is something highly different than having ...
0
votes
1
answer
411
views
How to write a struct with nested recursive data in golang
I have data like following
{
"cars": {
"toyota": [
"sedan",
"pickup"
],
"honda": [
"sedan",
"couple",
"pickup"
...
-1
votes
2
answers
780
views
When the form parameter in go is map, what is passed in?
When the formal parameter is map, assigning a value directly to a formal parameter cannot change the actual argument, but if you add a new key and value to the formal parameter, the actual argument ...
0
votes
1
answer
2k
views
Is writing to a mutex map with multiple goroutines faster than one? and why?
I have a SyncMap defined as follow:
type SyncMap struct {
sync.Mutex
Map map[int]string
}
And, now I write to it using two ways, one goroutine and multiple goroutines with mutex. codes as ...