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

我最近开始工作,golang因此很难理解如何实现我能够在Javaor中轻松完成的相同事情C#。我正在尝试创建一个configmanager类对象,当第一次configmanager调用类时,它应该初始化我的所有配置并将其存储在内存中的某个结构对象中。然后我应该可以访问configmanager对象,并且应该能够使用一些 getter 从我的主函数访问所有这些配置?


下面是我的configmanager围棋课。现在很简单,使其更容易理解。


package config


import (

    "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 main


import (

    "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 中完成我的上述代码?


HUH函数
浏览 93回答 3
3回答

大话西游666

这个问题很难回答,因为你有点忘了以明确的方式问一个问题,所以我将根据你写的内容提取一个问题来开始我的回答。我相信我们可以这样做:问题陈述[...]我的困惑是如何创建一个configmanager类对象以及我应该如何在我第一次调用时初始化我的所有配置,configmanager并且还可以使用一些 getter 访问ClientMetricsstruct我相信这里真正的问题是“我如何将读取和解组文件的关注与存储结果以供我的程序使用的关注区分开来?”。通过将事物分解为多个函数/方法来分离关注点是很常见的,您已经在某种程度上做到了这一点。然而,存储严格来说是类型的问题,所以我们需要一种能够保存结果的类型。我将利用这个机会Manager从类型名称中省略这个词,因为它所做的只是提供无用的抽象。这种类型不管理配置。它是配置,因为它包含所有配置。type Config struct {&nbsp; &nbsp; ClientMapConfigs&nbsp; &nbsp;[]ClientMapConfig&nbsp; &nbsp; DataMapConfigs&nbsp; &nbsp; &nbsp;[]DataMapConfig&nbsp; &nbsp; ProcessDataConfigs []ProcessDataConfig}请注意,这些字段以大写字母开头,使其公开。这表明其中可能存在废话,因为没有任何内容可以防止写入,这与我们从文件中读取这些数据的事实一致。正确的程序必须在使用之前验证这些数据。然后,您可以在变量名称中传达已验证数据的有效性。func main() {&nbsp; &nbsp; validConfig := getValidConfig("path/to/dir")&nbsp; &nbsp; // ...}func getValidConfig(configDirectoryPath string) *Config {&nbsp; &nbsp; config, err := NewConfigFromConfigDirectory(configDirectoryPath)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Printf("Failed to read config from dir '%s': %v\n", configDirectoryPath, err)&nbsp; &nbsp; &nbsp; &nbsp; os.Exit(1)&nbsp; &nbsp; }&nbsp; &nbsp; if err = ValidateConfig(config); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Printf("Config from dir '%s' failed to validate: %v\n", configDirectoryPath, err)&nbsp; &nbsp; &nbsp; &nbsp; os.Exit(1)&nbsp; &nbsp; }}func NewConfigFromConfigDirectory(configDirectoryPath string) *Config {&nbsp; &nbsp; // <- read individual configs and add to slices here&nbsp; &nbsp; return &Config{ // This is the closest to a "constructor" that Go has.&nbsp; &nbsp; &nbsp; &nbsp; ClientMapConfigs: clientMapConfigs,&nbsp; &nbsp; &nbsp; &nbsp; DataMapConfigs: dataMapConfigs,&nbsp; &nbsp; &nbsp; &nbsp; ProcessDataConfigs: processDataConfigs,&nbsp; &nbsp; }}请注意,验证和读取配置的函数不需要接收器,即成为结构的方法。它们可以作为独立功能使用,直到您的需求发生变化,需要为任一逻辑引入状态性。此外,我在这里对错误情况使用退出代码1,因为 Golang2在程序因恐慌而终止时使用代码。前者可以被认为是环境问题,而后者则表明程序本身存在问题。这是一个有用的区别,并且与您可能从 Java 中知道的语义Exception相一致。RuntimeException

GCT1015

我认为问题在于您从 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 herereturn &ClientMetric{ClientID,CustomerData, LegCustomer, OtherIds, CatID}, nil}在这种情况下,该函数NewClientMetric是构造函数,它返回一个指向新创建对象的指针/引用。它还返回一个错误对象,这和说构造函数抛出异常是一样的。正如您需要在 Java 中使用 try/catch 一样,您需要检查以处理此函数返回的 err 变量。您需要为每种类型创建类似的功能。至于 getter 和 setter,一般来说,在 Go 中应该避免这种情况。您可以直接访问结构的属性。仅当您要在返回属性之前对属性执行某些操作时,函数(如 getter)才有用。像这样的东西:type Customer struct {FirstName stringLastName string}func (this *Customer) GetFullName() string {return this.FirstName + " " + this.LastName}然后可以像这样访问这些:var customer *Customercustomer = &Customer{"John","Smith")fmt.Println(customer.FirstName)fmt.Println(customer.LastName)fmt.Println(customer.GetFullName())请注意,以大写字母开头的属性、函数和方法是公共的,其他的是私有的。如果FirstName写为firstName,则在声明它的包之外将无法访问它。请注意,如果指针为 null/nil,我不会检查错误,但始终建议这样做。

慕虎7371278

我可以说 10 个不同的配置(文件),每个配置都可以有自己的结构,因为它是不同的配置,所以我需要为它们有单独的结构这看起来像动态 JSON 结构解组,这是由John Asmuth在2015 年在使用混合结构进行解码时提出的您可以在此处运行以下示例。type Dog struct {&nbsp; Name&nbsp; &nbsp; &nbsp;string&nbsp; Frisbees int}type Cat struct {&nbsp; Name&nbsp; &nbsp; string&nbsp; Strings int}type RawAnimal struct {&nbsp; Type string&nbsp; Cat&nbsp; Dog}type Animal RawAnimalfunc (a *Animal) UnmarshalJSON(data []byte) error {&nbsp; if err := json.Unmarshal(data, (*RawAnimal)(a)); err != nil {&nbsp; &nbsp; return err&nbsp; }&nbsp; switch a.Type {&nbsp; case "cat":&nbsp; &nbsp; return json.Unmarshal(data, &a.Cat)&nbsp; case "dog":&nbsp; &nbsp; return json.Unmarshal(data, &a.Dog)&nbsp; }&nbsp; return fmt.Errorf("unknown type %q", a.Type)}从那里,您的 ConfigManager 将根据读取的原始 JSON 实例化正确的 Config 结构。
打开App,查看更多内容
随时随地看视频慕课网APP