These are my opinions, and others may disagree.
Up until now, I think what you did is tedious but acceptable. However,
I wouldn't write the Helpers struct, especially not for the reason you
mentioned. All these functions can either be written for Node, or they
can be standalone functions that take a Node parameter (or
NodeInterface). I don't think there is a need for a helpers interface.
Context would make sense if you had a Node (or NodeInterface) instead
of Helpers.
> ...
>
>
>
>
>
> Problems:
> 1. After using the `Context` methods to create a Transaction, it is necessary to `node.BroadcastTransaction()`. However, because of `HelpersInterface`, the code above cannot access the underlying Node instance.
> So I either create a new Node above, or return one from `Context`'s constructor
Get rid of Context.Helpers, instead use Context.Node.
>
> func NewContextFromURL(url string, address string, debug bool) (ctx *Context, node *Node)
>
>
>
> which seems inelegant.
>
> Of course I could add
> Helpers.BroadcastTransaction()
>
> but unlike the other helper methods, BroadcastTransaction() does not need any extra code around it.
>
> 2. After writing the question it seems like eliminating the Helpers struct will solve the problem. But I don't know if it is really THE solution, because I used the same pattern with NodeInterface, and sometimes I really do need to say that I will pass in a full-featured Node, so I have to have this large interface.
You should consider simply using Node in your code. If testing is a
concern and you cannot mock it, you can use NodeInterface.
>
> 3. Somehow I feel like the whole idea of making code easier to write by putting frequently used variables into a class/struct (yes I know Go has no classes) and calling the methods of that struct is not how Go is supposed to be used, but I am not sure.
That's not the purpose of structs. You should treat structs as data
types, with some cohesion in its fields and methods. It is not just a
collection of fields/methods.
>
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/3ee266e3-ccb4-401c-a754-addbda544349%40googlegroups.com.