为了账号安全,请及时绑定邮箱和手机立即绑定

如何制作一个类的对象并为结构设置吸气剂?

如何制作一个类的对象并为结构设置吸气剂?

Go
HUH函数 2022-10-24 09:52:44
我最近开始工作,golang因此很难理解如何实现我能够在Javaor中轻松完成的相同事情C#。我正在尝试创建一个configmanager类对象,当第一次configmanager调用类时,它应该初始化我的所有配置并将其存储在内存中的某个结构对象中。然后我应该可以访问configmanager对象,并且应该能够使用一些 getter 从我的主函数访问所有这些配置?下面是我的configmanager围棋课。现在很简单,使其更容易理解。package configimport (    "encoding/json"    "fmt"    "io/ioutil"    "github.com/david/internal/utilities")type ClientMetrics struct {    ClientMetrics []ClientMetric `json:"clientMetrics"`}type CustomerData struct {    Process []string `json:"Process"`    Mat     []string `json:"Mat"`}type ClientMetric struct {    ClientID     int          `json:"clientId"`    CustomerData CustomerData `json:"customerData,omitempty"`    LegCustomer  []int        `json:"legCustomer"`    OtherIds     []int        `json:"otherIds,omitempty"`    CatID        int          `json:"catId,omitempty"`}func Init(root string) (err error) {    files, err := utilities.FindFiles(root, "process-data.json")    if err != nil {        return fmt.Errorf("cannot find process-data.json file: %v", err)    }    for _, file := range files {        body, err := ioutil.ReadFile(file)        if err != nil {            return fmt.Errorf("unable to read file: %v", err)        }        var auto ClientMetrics        json.Unmarshal(body, &auto)    }    return nil}这是我在我的主要功能中使用它 - 这只是基本代码,只是为了演示我在做什么,但这不是production现成的代码。package mainimport (    "github.com/david/internal/config")func main() {    root := "/home/Configurations"    config.Init(root)  //}在我上面的Init函数中,process-data.json如果文件在磁盘上,我会找到它,然后通过将其反序列化为ClientMetrics对象将其加载到内存中。如上所示,一切正常。问题陈述由于我来自背景Java,C#所以我的困惑是如何创建一个configmanager类对象,以及如何在我第一次调用时初始化我的所有配置,并且还可以使用一些 getterconfigmanager访问结构。ClientMetrics在 Java 和 C# 中,我曾经使用构造函数来初始化所有这些东西,然后使用一些 getter 来访问配置。我应该如何在 golang 中做同样的事情。我的主要困惑是我们在 go 中是否有构造函数,我应该如何让 getter 在我的 main 函数中访问 struct 对象?我只是在寻找更好的设计,我应该如何在 golang 中完成我的上述代码?
查看完整描述

3 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

这个问题很难回答,因为你有点忘了以明确的方式问一个问题,所以我将根据你写的内容提取一个问题来开始我的回答。我相信我们可以这样做:


问题陈述


[...]我的困惑是如何创建一个configmanager类对象以及我应该如何在我第一次调用时初始化我的所有配置,configmanager并且还可以使用一些 getter 访问ClientMetricsstruct


我相信这里真正的问题是“我如何将读取和解组文件的关注与存储结果以供我的程序使用的关注区分开来?”。


通过将事物分解为多个函数/方法来分离关注点是很常见的,您已经在某种程度上做到了这一点。然而,存储严格来说是类型的问题,所以我们需要一种能够保存结果的类型。我将利用这个机会Manager从类型名称中省略这个词,因为它所做的只是提供无用的抽象。这种类型不管理配置。它是配置,因为它包含所有配置。


type Config struct {

    ClientMapConfigs   []ClientMapConfig

    DataMapConfigs     []DataMapConfig

    ProcessDataConfigs []ProcessDataConfig

}

请注意,这些字段以大写字母开头,使其公开。这表明其中可能存在废话,因为没有任何内容可以防止写入,这与我们从文件中读取这些数据的事实一致。正确的程序必须在使用之前验证这些数据。然后,您可以在变量名称中传达已验证数据的有效性。


func main() {

    validConfig := getValidConfig("path/to/dir")

    // ...

}


func getValidConfig(configDirectoryPath string) *Config {

    config, err := NewConfigFromConfigDirectory(configDirectoryPath)

    if err != nil {

        log.Printf("Failed to read config from dir '%s': %v\n", configDirectoryPath, err)

        os.Exit(1)

    }

    if err = ValidateConfig(config); err != nil {

        log.Printf("Config from dir '%s' failed to validate: %v\n", configDirectoryPath, err)

        os.Exit(1)

    }

}


func NewConfigFromConfigDirectory(configDirectoryPath string) *Config {

    // <- read individual configs and add to slices here


    return &Config{ // This is the closest to a "constructor" that Go has.

        ClientMapConfigs: clientMapConfigs,

        DataMapConfigs: dataMapConfigs,

        ProcessDataConfigs: processDataConfigs,

    }

}

请注意,验证和读取配置的函数不需要接收器,即成为结构的方法。它们可以作为独立功能使用,直到您的需求发生变化,需要为任一逻辑引入状态性。


此外,我在这里对错误情况使用退出代码1,因为 Golang2在程序因恐慌而终止时使用代码。前者可以被认为是环境问题,而后者则表明程序本身存在问题。这是一个有用的区别,并且与您可能从 Java 中知道的语义Exception相一致。RuntimeException


查看完整回答
反对 回复 2022-10-24
?
GCT1015

TA贡献1827条经验 获得超4个赞

我认为问题在于您从 Java/C# 的角度看待 Go,因此难以理解这些特性。如果您有时间,那么我建议您在开始编码之前先阅读一些 Go 教程或书籍(这个非常好:https ://www.amazon.com/Programming-Language-Addison-Wesley-Professional-计算/dp/0134190440 )


要直接回答您的问题,您需要做的是创建一个函数,该函数返回指向结构对象的指针(请参见此处的简单示例:https ://gobyexample.com/structs )


以 ClientMetric 为例:


func NewClientMetric(ClientID int, CustomerData CustomerData, LegCustomer []int, OtherIds []int, CatID int) (*ClientMetric, error) {


//validate your inputs

//in case of errors, create and error message in the variable err and then: return nil, err

//other code which would go into a typical constructor would go here


return &ClientMetric{ClientID,CustomerData, LegCustomer, OtherIds, CatID}, nil

}

在这种情况下,该函数NewClientMetric是构造函数,它返回一个指向新创建对象的指针/引用。它还返回一个错误对象,这和说构造函数抛出异常是一样的。正如您需要在 Java 中使用 try/catch 一样,您需要检查以处理此函数返回的 err 变量。


您需要为每种类型创建类似的功能。


至于 getter 和 setter,一般来说,在 Go 中应该避免这种情况。您可以直接访问结构的属性。仅当您要在返回属性之前对属性执行某些操作时,函数(如 getter)才有用。像这样的东西:


type Customer struct {

FirstName string

LastName string

}


func (this *Customer) GetFullName() string {

return this.FirstName + " " + this.LastName

}

然后可以像这样访问这些:


var customer *Customer

customer = &Customer{"John","Smith")

fmt.Println(customer.FirstName)

fmt.Println(customer.LastName)

fmt.Println(customer.GetFullName())

请注意,以大写字母开头的属性、函数和方法是公共的,其他的是私有的。如果FirstName写为firstName,则在声明它的包之外将无法访问它。


请注意,如果指针为 null/nil,我不会检查错误,但始终建议这样做。


查看完整回答
反对 回复 2022-10-24
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

我可以说 10 个不同的配置(文件),每个配置都可以有自己的结构,因为它是不同的配置,所以我需要为它们有单独的结构

这看起来像动态 JSON 结构解组,这是由John Asmuth在2015 年在使用混合结构进行解码时提出的

您可以在此处运行以下示例

type Dog struct {

  Name     string

  Frisbees int

}

type Cat struct {

  Name    string

  Strings int

}

type RawAnimal struct {

  Type string

  Cat

  Dog

}

type Animal RawAnimal

func (a *Animal) UnmarshalJSON(data []byte) error {

  if err := json.Unmarshal(data, (*RawAnimal)(a)); err != nil {

    return err

  }

  switch a.Type {

  case "cat":

    return json.Unmarshal(data, &a.Cat)

  case "dog":

    return json.Unmarshal(data, &a.Dog)

  }

  return fmt.Errorf("unknown type %q", a.Type)

}

从那里,您的 ConfigManager 将根据读取的原始 JSON 实例化正确的 Config 结构。


查看完整回答
反对 回复 2022-10-24
  • 3 回答
  • 0 关注
  • 84 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信