3 回答

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

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,我不会检查错误,但始终建议这样做。

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 结构。
- 3 回答
- 0 关注
- 125 浏览
添加回答
举报