英文:
Object orientation in Go - Struct/Composition/ Encapsulation
问题
我有两个.go文件 - client.go(包含主要函数)和logic.go。其中一个包含在从客户端调用时需要执行的函数。
client.go -
package main
func main() {
// url是发送REST调用以获取响应的服务器的URL
client := NewClient(url)
client.DummyFunc()
}
logic.go
import (
"fmt"
)
func DummyFunc(){
// 在这里编写返回JSON响应的逻辑
}
我正在尝试理解在Go中以面向对象的方式完成这个任务的好方法。由于Go有自己的对象继承/组合/封装风格,您能否建议我一种方法来完成这个任务,因为我对这门语言还不熟悉。
另外,我希望client.go和logic.go中的main函数包含其他可以从client.go文件中调用的实用函数。
英文:
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.
答案1
得分: 1
在讨论以面向对象的方式解决这个问题之前,client.go
是否需要包含main
函数?logic.go
是否需要与client.go
分离?此外,logic.go
必须是一个包的一部分。我很难确定哪些是为了简洁而省略的,哪些可能是缺失的。
我会这样做:
main.go(对于两种情况都相同)
package main
import "client"
func main() {
client := client.Client { ClientUrl: url } // 这取代了NewClient(url)
client.DummyFunc()
}
client.go
package client
type Client struct {
ClientUrl string
}
func (c Client) DummyFunc() {
// 在这里处理逻辑并返回JSON响应
}
如果你想将logic
与client
分开,你可以这样做。
client.go
package client
type Client struct {
ClientUrl string
Logic // 匿名字段允许你调用Logic函数
}
logic.go
package client
type Logic struct {
// 字段
}
func (l Logic) DummyFunc() {
// 在这里处理逻辑并返回JSON响应
}
请注意,由于我们在Client
结构体中将Logic
设置为匿名字段,我们可以调用DummyFunc
。
你可以在这里看到更多的例子:http://golangtutorials.blogspot.com/2011/06/inheritance-and-subclassing-in-go-or.html
编辑--------------------------------------------------------
这有点尴尬,但是client.go
中包含了main
函数,而logic.go
是独立的。如果你在logic
中添加更多的函数,那么client
将能够调用所有其他函数。
client.go
package main
import "logic"
type Client struct {
ClientUrl string
logic.Logic
}
func main() {
client := client.Client { ClientUrl: url } // 这取代了NewClient(url)
client.DummyFunc()
}
logic.go
package logic
import "fmt"
type Logic struct {
// 字段
}
func (l Logic) DummyFunc() {
fmt.Printf("In DummyFunc\n")
}
话虽如此,我不确定这是否是最佳的方法。如果logic
没有任何字段,那么将其作为一个独立的结构体是没有意义的。我会将其作为client
的一个函数,就像我在原始帖子的第一种情况中指出的那样。
英文:
Before addressing the OO way of doing this, does client.go
need to hold the main
function? Does logic.go
need to be separated from client.go
? Also, logic.go
must 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)
package main
import "client"
func main() {
client := client.Client { ClientUrl: url } // This takes the place of NewClient(url)
client.DummyFunc()
}
client.go
package client
type Client struct {
ClientUrl string
}
func (c Client) DummyFunc() {
// Logic here which returns the JSON response
}
If you would like to keep logic
separate from client
, you could do this.
client.go
package client
type Client struct {
ClientUrl string
Logic // An anonymous field let's you call Logic functions
}
logic.go
package client
type Logic struct {
// Fields
}
func (l Logic) DummyFunc() {
// Logic here which returns the JSON response
}
Notice, since we kept Logic
anonymous in the Client
struct, we were able to call DummyFunc
.
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.go
with main function in it and logic.go
is separate. If you add more functions in logic
, then client
will be able to call all other functions.
client.go
package main
import "logic"
type Client struct {
ClientUrl string
logic.Logic
}
func main() {
client := client.Client { ClientUrl: url } // This takes the place of NewClient(url)
client.DummyFunc()
}
logic.go
package logic
import "fmt"
type Logic struct {
// Fields
}
func (l Logic) DummyFunc() {
fmt.Printf("In DummyFunc\n")
}
With that being said, I'm not sure this is the best way to do it. If logic
doesn't have any fields, then it makes no sense to have it as a separate struct. I would make it a function of client
, as I pointed out in the first case in the original post.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论