Cross-cutting concerns in Go?

1,061 views
Skip to first unread message

Kamil Chmielewski

unread,
Jan 13, 2015, 2:49:45 AM1/13/15
to golan...@googlegroups.com
Hi,

I'm trying to rewrite one of my clis from Python to Go. I found https://github.com/spf13/cobra that looks like the simplest library to do it.
The cli that I write have few cross-cutting concerns, like logging all commands execution with current environment data (OS, hostname ...). In my Python code I use decorators to wrap specific code with additional behaviour.
I'm new to Go and I thought I can try to do the same with cobra embedding its Command struct in my ExtendedStruct overridding/wrap its Execute function. I ended with this https://play.golang.org/p/u7ZhIM0TIm
How would you attach additional behaviour to functions like this in Go and don't duplicate cross-cutting concerns code?

Thanks.

Egon

unread,
Jan 13, 2015, 3:04:42 AM1/13/15
to golan...@googlegroups.com
Short answer, "It depends".

Currently you are explaining an abstract problem and not a concrete problem, which means giving a good solution is really difficult. Basically, there is not enough information to give a good answer here. Take a look at HowToAsk to see what information is usually necessary and which helps people understand your problem better.

If the ExtendedCommand solves your problem, then it's an OK solution, of course there could be better solutions.

+ Egon

Kamil Chmielewski

unread,
Jan 13, 2015, 3:35:25 PM1/13/15
to golan...@googlegroups.com
I try to be more specific. I want to use Cobra library to write cli in a way that every command execution is logged with current environment information.
I tried to use ExtendedCommand from my example but I get compiler error: cannot use type ExtendedCommand as type CobraCommand.
How can I write this in idiomatic Go?

Kamil

Egon

unread,
Jan 13, 2015, 3:54:41 PM1/13/15
to golan...@googlegroups.com
Print the current environment before Execute.

Alternatively wrap the run func

+ Egon
 

Kamil

benn

unread,
Jan 13, 2015, 8:19:01 PM1/13/15
to golan...@googlegroups.com
I started this as a generic way of decorating arbitrary functions: https://github.com/bennAH/decorator

Any feedback/contributions are welcome.

Egon

unread,
Jan 14, 2015, 2:12:37 AM1/14/15
to golan...@googlegroups.com
On Wednesday, 14 January 2015 03:19:01 UTC+2, benn wrote:
I started this as a generic way of decorating arbitrary functions: https://github.com/bennAH/decorator

Any feedback/contributions are welcome.

Bad idea, this approach is fighting the type-system. When you need it you can write the type-safe approach trivially.

This is much simpler than using a reflection based approach.

+ Egon

Kamil Chmielewski

unread,
Jan 15, 2015, 1:32:37 AM1/15/15
to golan...@googlegroups.com

W dniu wtorek, 13 stycznia 2015 21:54:41 UTC+1 użytkownik Egon napisał:

Print the current environment before Execute.

Alternatively wrap the run func


I like the solution with wrap function and I'll go with it.
Thx Egon!
Reply all
Reply to author
Forward
0 new messages