Go lang doesn't have a lot of syntactical sugar, but it has a few tricks up its sleeve every once in a while. Today, for example, I realized you can extend any arbitrary type so that it has customized methods over it. This is a fast and clean way to build scoped and encapsulated operations on top of primitives without having to wrap them in structs.
glog.V(...) is normally used to return a logging object, so that you can log something at a certain level.
glog.V(3).Info("noisy log")
Today I found that in golang, you can add methods to primitives.
This is an interesting feature which allows you to build declarative constructs on top of a number, a boolean, etc...
So you can do things like this.
if glog.V(3) {
// do something.
}
As well as accessing the interfaces which the type "V" implements.
When should you use this?
Any time that you have a specific flag, or value, which has certain operations that it should be entirely in control of, you can use the "type" syntax in go to make your own, method extensible primitive.
No comments:
Post a Comment