On Thu, 30 Aug 2018 18:41:48 -0700 (PDT)
Eric Raymond <e...@thyrsus.com> wrote:

> Oh, actually, I have one other issue; how to translate virtual methods in a 
> Python base class into Go's object model.

Inheritance based domain models do not translate directly to go's composition
based ones. Forget "virtual". Forget inheritance.

There is a go generate[1] that helps with rewriting polymorphic constructs.
There is a whole python->go transpiler [2] too that can help with code
snippets. (Seems stale though).


[Personal (perl->go) story/thoughts/notes ahead. YMMV.]

For me at way less daunting task the short focused transpilers written in a
language I was fluent with helped significantly **to get things work** in go
as they were in perl. When I had ugly but working (passing comparative tests)
translation I could refactor it over time touching only transpiler templates
as I learned more about new language's constructs and idioms. Then there was
a point I was ready to re-write more and more in hand-written go.


Lessons learned:

 * Do NOT emulate inheritance! Its dead end.

 * Do not use reflection unless you really are unable to find other ways. Its
slow, its clumsy, it allows for shortcuts but through the swamps.

 * Translate every and each **class hierarchy** to a separate go package
first. Then try to shrink exposed API. Ideas of how to do something go-way
will emerge naturally in the process.

 * Type switch is fast but direct call is both faster and easier to grasp
later[*].

 * Never return interface. (Ok, unless you're a go guru making stdlibs).

 * Go's regexp engine is, umm, "other kind of". Do not use it unless your
 code has plenty of time to go ( ok, could be its a former camel pain ;) 

 * Dig into source of go and highly revered libs (eg. [3]) for inspirations.

 * Do NOT emulate inheritance! Its a footcannon with cannonballs so huge that
they can shoot away titanosaur's leg. :)

[Criticism and addenums from more experienced gophers welcome!]


[1] old but concise: 
https://blog.gopheracademy.com/advent-2015/reducing-boilerplate-with-go-generate/
https://github.com/myitcv/x/blob/master/gogenerate/_doc/README.md

[2] https://github.com/rusthon/Rusthon

[3] https://github.com/Workiva/go-datastructures

[*] A dozen of func chkValid_MTxyOK (x typeMTxy) proved to be easier to
maintain than a single chkValidMT(x MTcheckerInterface) one. Now common
 checks are factored out and all "checkable" structs have 'IsOK' methods that
 call relevant series of "generic" checks but this would not be done without
 previous translation from hierarchy to separate typed funcs)


Good luck!

-- 
Wojciech S. Czarnecki
 << ^oo^ >> OHIR-RIPE

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to