使用统一建模语言图对Go进行编程
I have just started using Go (GoLang) and I am finding it a great language. However, after many years of UML and the Object Oriented methods, I find that modelling Go programs (Reverse engineering) is a bit problematic, in that Go Structs contain properties/state, but no methods, and methods/functions that use Structs as parameters (even the ones that do magic so that it makes a Struct look like an object), don't contain methods, or state.
Does this mean I should be using another Methodology to model a Go Program or does UML sufficiently model the language constructs?
Yes I know that if you use methods on the Structs that the behavior of an object in UML can be mapped into Go via a combination of a Struct and a Struct Method, but I am finding this to be wrong, an impedance mismatch in paradigms of sorts.
Is it time for a new (perish the thought!) diagramming technique, for the brave new world where behavior is no longer controlled by an object? can behavior be modeled without reference to the state that it is affecting?
Update:
I am trying Data Flow Diagrams out, to see if they fit better to the paradigm. So far so good, but I think I am going to come unstuck when I model the Methods of a Struct, the compromise in the DFD being that they are treated as Functions. :(
Go supports inheritance!!! arghhh!!! (head is blown clean off.) you can compose a Struct which is made of another Struct, which has methods, that the Sub Struct now inherits...you getting this? my mind is blown. Means that UML IS valid...fully but it feels dirty.
Go Does not support inheritance, it just appears so. :) DFD's it is then!
我刚开始使用Go(GoLang),并且发现它是一种很棒的语言。 但是,经过多年的UML和面向对象的方法,我发现建模Go程序(逆向工程)有点麻烦,因为Go Structs包含属性/状态,但是没有方法以及使用Structs作为参数的方法/函数 (甚至是做魔术的人,也就是使Struct看起来像对象的人),不包含方法或状态。 p>
这是否意味着我应该使用其他方法论进行建模 是Go程序还是UML足以对语言结构进行建模? p>
是的,我知道,如果您在Structs上使用方法,则可以通过组合将UML中对象的行为映射到Go中 Struct和Struct方法,但我发现这是错误的,是各种范例中的阻抗不匹配。 p>
现在该是一种新的(消灭思想!)绘图技术的时候了 ,对于行为不再由对象控制的勇敢的新世界? 可以在不参考行为影响状态的情况下对行为进行建模吗? p>
更新: strong> p>
我正在尝试数据流程图,以查看它们是否更适合该范式。 到目前为止,一切都很好,但是当我对结构方法进行建模时,我想我不会感到困惑,在DFD中的折衷之处在于将它们视为函数。 :( p>
Go支持继承!!! arghhh !!!(将头吹干净)。您可以编写一个由另一个Struct组成的Struct,该Struct具有方法,即Sub 现在Struct继承了...你明白了吗?这让我很震惊。这意味着UML是有效的...但是它感觉很脏。 p>
Go不支持继承,它看起来像这样 。:) DFD就是这样! p>
div>
UML still gives you tools useful for analysis and design of components, interfaces, and data. Go is not an OO language, so you cannot use inheritance, polymorphism, methods, etc. You don't need a new paradigm, you may need an old one: Structured Analysis and Structured Design.
If you like planning and modeling more than programming you should just stick with Java.
If you like building and maintaining the actual code and working systems you should try just planning your Go program on a piece of paper or a whiteboard and get programming.
The fact that methods are declared outside the definition of the struct itself should not be significant; it is just a minor syntax difference. The methods belong to the struct type just as surely as if they were inside the braces. (They are declared outside the braces because methods aren't limited to structs.)
The real potential problem with using UML with Go is that UML is normally used with traditional object-oriented design (i.e. class hierarchies), and Go takes a different approach to inheritance and polymorphism. Maybe UML can be adapted to Go's type system—I'm not familiar enough with UML to say—but your design approach will probably need to change somewhat whether you continue using UML or not.
Go is not intended to be used in the everything-is-an-object style of Smalltalk and Java. Idiomatic Go programs generally contain a large percentage of procedural code. If your design process focuses on object modeling, it will not work well for Go.
Go Structs contain properties/state, but no methods, and methods/functions that use Structs as parameters (even the ones that do magic so that it makes a Struct look like an object), don't contain methods, or state.
As you may know, in C++ you can also declare methods on structs - just as in classes with the only difference that you won't be able to use access modifiers.
In OOP languages you declare methods of a class inside the class definition, giving the feeling that these methods are somehow part of the class. This is not true for most languages and Go makes this obvious.
When you declare something like the following pseudo-code in a traditional OOP language:
class Foo {
public function bar(x int) {
// ...
}
}
the linker will export a function that will look something like:
Foo__bar(this Foo, x int)
When you then do (assume f
is an instance of Foo
):
f.bar(3)
you are in fact (and indirectly, more on that later) doing:
Foo__bar(f, 3)
The class instance itself will only contain a so called vtable with function pointers to the methods it implements and/or inherits.
Additionally, methods do not contain state, at least not in the contemporary programming world.
Does this mean I should be using another Methodology to model a Go Program or does UML sufficiently model the language constructs?
UML should suffice.
Is it time for a new (perish the thought!) diagramming technique, for the brave new world where behavior is no longer controlled by an object?
Naa.
Can behavior be modeled without reference to the state that it is affecting?
Yes, that's what interfaces are for.
I am trying Data Flow Diagrams out, to see if they fit better to the paradigm. So far so good, but I think I am going to come unstuck when I model the Methods of a struct, the compromise in the DFD being that they are treated as Functions. :(
Do not get lost in abstractions, break them. There is no perfect paradigm and there will never be.