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
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:
Creating a Constant Type and Restricting the Type's Values
(2 answers)
What is an idiomatic way of representing enums in Go?
(14 answers)
Closed 8 months ago.
I have a struct Direction with a value of type string. Direction should be N, S, W or E.
type Direction struct {
value string
}
Inspired by an answer of this question: Does Go have "if x in" construct similar to Python? I guess a good way to create Direction in a valid manner can be this one:
func NewDirection(direction string) Direction {
switch direction {
case "N","S","W","E": return Direction{direction}
}
panic("invalid direction")
}
But this is not enough for me because I can still create invalid Direction:
d := Direction{"X"}
I also found this interesting article about enforcing the use of constructor in go. Reading this article I can see that is necessary the usage of another package. May I have a "protected" struct in, for example, main package?
You've already done almost everything you should do by convention:
make the field unexported
provide a constructor
put a comment on the type stating that the constructor should be used, and explain how zero-values are treated (if that matters)
Now users of the package can't modify the field, and the existence of the constrictor makes it clear that it should be called to create valid instances. This is the convention that is set by the standard library.
Sure, there are other ways that you can make it even harder to invalidate values but this is essentially just wasting time and overcomplicating your code for the sake of an unwinnable arms race against an imaginary opponent.
If someone doesn't understand the language and doesn't read documentation, then they're always going to find a way to misuse it. If they are actively trying to sabotage themselves, there's nothing you can do to stop them and no reason to either.
Packages are the smallest functional unit of code organization in Go. There is no way to protect field at, for example, the file level. Even files within the same package effectively operate as if all their code was in the same file. Therefore, any code in the same package as the constructor will have the same privileges as the constructor.
This question already has answers here:
What are the rules for re-binding?
(3 answers)
Closed 1 year ago.
[EDIT: closed in favor of https://stackoverflow.com/questions/69231506/what-are-the-rules-for-re-binding, which I formulated after more clearly understanding what I was trying to ask in this question.]
My understanding from Is there a purpose or benefit in prohibiting sigilless variables from rebinding? was a symbol declared without a sigil could never be rebound. Quoting from that answer:
Yes, [the current behavior is] certainly by design, and - like most things in [Raku] design - it's this way for more than one reason.… It was decided to make the sigilless symbol form a "static single assignment" syntax…. There were various reasons for this, including… enhancing program readability by having a form that lets the reader know that the symbol will never be rebound to a new value
(emphasis added.)
Given that, I was very surprised to see by the code below:
my Int \b = 8;
say "{b*b}"; # OUTPUT: «64»
b := 4;
say "{b*b}"; # OUTPUT: «16»
That is, when b is declared without a sigil but with an Int type constraint, it can be rebound – unlike when it lacks that type constraint. Is this behavior a bug, or is it correct?
If it is, how does it fit in with the design considerations mentioned in the answer linked above?
(See also this Raku/doc issue thread on GitHub for a discussion of this behavior and whether it's intentional.)
It's a bug.
[no language should sometimes prohibit sigilless variables from rebinding depending on whether a or which type is specified in the declaration].
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.
This question already has answers here:
Why isn't short variable declaration allowed at package level in Go?
(3 answers)
Closed 8 years ago.
Coming from a background in Java and C# I am very pleased with the brevity of Golang's ability to use the shortcut method for variable declaration for private variables within functions, which allows me to write:
x := 1.5
It reminds me of duck typing in a dynamic language such as Python. However, in declaring global variables outside of the scope of a function you still need to use the more verbose syntax of:
var x float64 = 1.5
I'm just wondering why the shortcut method works for private variables and not globals? I know that the designers of the language are quite experienced so I'm assuming that this isn't reflective of a feature overlooked. Is there a technical reason why this kind of type inference (and I realize that the := shortcut is not the same as proper type inference) wouldn't work at the global scope? It just seems somewhat inconsistent in terms of the design, and as an inexperienced Gopher I must admit to being thrown off by this on a couple of occasions. On the whole however I'm really enjoying Go.
See the Ian's answer in this thread:
https://groups.google.com/forum/#!msg/golang-nuts/qTZemuGDV6o/IyCwXPJsUFIJ
At the top level, every declaration begins with a keyword. This
simplifies parsing.
Actually you don't need to specify type in many cases.
var x = 1.5
should work fine. It's probably as short as it can get and it's not much longer than local variable shortcut.
So there is a shortcut for global.
As to why := can't be used, I would guess that calling out var makes code structure more consistent as other global constructs start with a keyword - func, const, import, type.