Go语言中的面向对象编程 – 结构体/组合/封装

huangapple go评论80阅读模式
英文:

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响应
}

如果你想将logicclient分开,你可以这样做。

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.

huangapple
  • 本文由 发表于 2014年11月16日 14:34:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/26954536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定