I have two .go files - client.go ( contains the main fund) and logic.go. One of them contains the function which needs to be executed when it is invoked from the client. {
client.go -
package main
func main() {
// url is the url of the server to which the REST call has to be sent to fetch the response
client := NewClient(url)
client.DummyFunc()
}
logic.go
import (
"fmt"
)
func DummyFunc(){
// Logic here which returns the json response
}
}
I am trying to understand what could be a good object oriented way in Go to do this. Since Go has its own style of object inheritance/composition/encapsulation, could you please suggest me a way to do this as I am new to this language.
Also, I want the main function in my client.go and logic.go should contain other utility functions which can be invoked from my client.go file.
Before addressing the OO way of doing this, does
client.goneed to hold themainfunction? Doeslogic.goneed to be separated fromclient.go? Also,logic.gomust be part of a package. I'm having a hard time picking up what was left out for brevity versus what may be missing.How I would do things:
main.go (would be the same for both cases)
client.go
If you would like to keep
logicseparate fromclient, you could do this.client.go
logic.go
Notice, since we kept
Logicanonymous in theClientstruct, we were able to callDummyFunc.You can see some more examples here: http://golangtutorials.blogspot.com/2011/06/inheritance-and-subclassing-in-go-or.html
EDIT --------------------------------------------------------
It's a little awkward, but here's
client.gowith main function in it andlogic.gois separate. If you add more functions inlogic, thenclientwill be able to call all other functions.client.go
logic.go
With that being said, I'm not sure this is the best way to do it. If
logicdoesn't have any fields, then it makes no sense to have it as a separate struct. I would make it a function ofclient, as I pointed out in the first case in the original post.