This question already has answers here:
Generic hashmap in Go
(3 answers)
Closed 8 months ago.
Given that I have a MyMap variable of type
*map[uuid.UUID][]*Thing
and a piece of code like this:
h.MyMap[id] = append(h.MyMap[id], &thingA)
// other stuff with MyMap like this, later:
h.MyMapp[id][k] = &thingB
// and so on... the actual code is way more complex
and an identical code like above, except the fact it uses a map of type:
*map[int64][]*Thing
Is there a Golang way to DRY it and write a helper that does the similar code in one place?
Thank you.
[I]s there a Golang way to DRY it and write a helper that does the similar code in one place?
No.
Related
This question already has an answer here:
Is this casting in golang?
(1 answer)
Closed 7 months ago.
After having issues understanding and being unable to access the ClusterComputeResourceSummary.UsageSummary field while working with govmomi, I was able to find a link that helped solve my problem, however, I am curious to understand how Golang is doing this behind the scenes, how after a chaining dot you provide the type you must obviously know before hand to extract the object and its properties?
usage := resource.Summary.(*types.ClusterComputeResourceSummary).UsageSummary
How can I go about reading this syntax, especially the part after .(*types.ClusterComputeResourceSummary) ?
P.S. Forgive me for the question title, honestly I don't know the right term or lingo for this use case, like is this reflection or something similar?
This is a "type assertion". The resource.Summary is an interface value that contains a pointer to an object, and its type. The type assertion checks if the type you gave *types.ClusterComputeResourcesSummary can be assigned to the type of data stored in the interface, and if so, returns the value stored in the interface as an instance of that type. Then you can access the members/methods of that variable.
This question already has answers here:
How to test the passing of arguments in Golang?
(2 answers)
Closed 4 years ago.
Is it possible to change the value of os.Args globally in a Golang program? I'm writting a library like Cobra just for fun, and internally I make use of os.Args to parse the command-line arguments. I would like to do that to properly test the parser.
Currently what I am doing is manually changing the internal variable of my package, but that's a bad practice.
Thank you.
I think the answer to the previous question How to test the passing of arguments in Golang? describes how to modify the os.Args during testing.
tomasz states " the very first value in os.Args is a (path to) executable itself, so os.Args = []string{"cmd", "-user=bla"} should fix your issue. You can take a look at flag test from the standard package where they're doing something similar."
This question already has answers here:
What is this "err.(*exec.ExitError)" thing in Go code? [duplicate]
(2 answers)
What is the meaning of "dot parenthesis" syntax? [duplicate]
(1 answer)
Closed 5 years ago.
Go Newb here -- I've encountered the following bit of Go code that I didn't write
if tc, ok := tng.(ThingClasser); ok {
//... do some stuff ...
}
I won't understand the semantics of tng.(ThingClasser).
In some ways this looks like a method call -- i.e. there are two variables (ec, ok) sitting there ready to accept multiple return values.
However, tng.(ThingClasser) itself looks like its a property access, not a method call.
However however, the parens around ThingClasser are a wrinkle I've never seen before. Also, if it matters, the ThingClasser symbol is defined elsewhere in this project as an interface, so I think maybe this is some syntactic sugar around an does this implement an interface -- but then the two return values confused me.
Googling hasn't turned up anything concrete, but this is one of those hard things to google for.
Does anyone here know what this call/syntax is in GoLang, and possible point me at the relevant manual pages so I can RTFM?
It's a type assertion. The returned values are 1) the object, converted to the given type; and 2) a boolean indicating if the conversion was successful. ThingClasser is the type being converted to. Documentation can be found here: https://golang.org/ref/spec#Type_assertions
This question already has answers here:
How can I build an enum with Dart? [duplicate]
(4 answers)
Closed 8 years ago.
A simple question here :) I'm really happy to see a new feature in the dart language. But I just realized that I kind of never use enumeration.I don't know if there is a discussion somewhere about that but.
What is the pros and cons of this feature in terms of code writing (seems shorter), performance,etc?
Cheers
If I understand it correctly an enum is like a class with const members and some useful methods. Given that cost members are resolved by the compiler using an enum should not incur in any performance hit.
In terms of "code writing" enums are good candidates to replace classic const or static enumerations or, even better, hard-coded constants.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Placement of the asterisk in Objective-C
I'm new to XCode, coming from C#. What is the difference between the two following syntax examples, specifically the location of the asterisk?
UITabBarItem* tabBarItem
and
UITabBarItem *tabBarItem
When do you use one syntax over the other?
Both mean the same. There is no difference between the two and it is a matter of preference. I personally prefer the second way because it looks cleaner when have multiple pointers.
UITabBarItem *tabBarItemOne, *tabBarItemTwo ; // Looks cleaner :)
than
UITabBarItem* tabBarItemOne, *tabBarItemTwo ;
There is no difference, just preference. Both declare a pointer to a UITabBarItem.