Golang中如何声明内部类型结构体的变量

Golang中如何声明内部类型结构体的变量 你好。我从一个.NET系统接收数据,并正在将其迁移到GO。 他们要求我保持相同的数据结构。 我进行了转换并得到了:

type GetAllLineTypeOfTbPriceScheduleViewModel struct {
	Integration struct {
		GetAllLineTypeOfTbPriceSchedule struct {
			Database struct {
				Rows struct {
					Row []struct {
						LineType string `json:"LineType"`
					} `json:"Row"`
				} `json:"Rows"`
			} `json:"Database"`
		} `json:"GetAllLineTypeOfTbPriceSchedule"`
	} `json:"Integration"`
}

现在我想创建一个行类型的切片,并像这样声明:

result := entities.GetAllLineTypeOfTbPriceScheduleViewModel{}
var row result.Integration.GetAllLineTypeOfTbPriceSchedule.Database.Rows.Row

然而,Go编译器在第一个点处标记了一个错误。看起来它会被当作包定义…

是否有另一种方法可以访问内部结构,而无需将其声明为外部结构?

提前致谢。


更多关于Golang中如何声明内部类型结构体的变量的实战教程也可以访问 https://www.itying.com/category-94-b0.html

7 回复

不,你需要将它们声明为实际命名的类型。

更多关于Golang中如何声明内部类型结构体的变量的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


是的,雅各布。非常感谢!!!

好的。我重新检查了一下,问题就在这里。一个拼写错误!!! 非常感谢!!!

Go 不会那样做,但这可能是一个容易拼错的长标识符,或者文件没有正确保存。

我解决了这个问题!!! 看起来 GetAllLineTypeOfTbPriceSchedule 这个名称太长了,Go 编译器使用变量名的前 “n” 个字符,所以它可能会与文件中的其他标识符混淆。 所以我学到了,要保持简洁,不要过于冗长……!… 😊

再提一个问题。我已经将那个巨型结构体拆分成了多个较小的结构体:

type GetAllLineTypeOfTbPriceScheduleViewModel struct {
	Integration IntegrationRec `json:"Integration"`
}

type IntegrationRec struct {
	GetAllLineTypeOfTbPriceSchedule GetAllLineTypeOfTbPriceScheduleRec `json:"GetAllLineTypeOfTbPriceSchedule"`
}

type GetAllLineTypeOfTbPriceScheduleRec struct {
	Database DatabaseRec `json:"Database"`
}

type DatabaseRec struct {
	Rows RowRec `json:"Rows"`
}

type RowRec struct {
	Row []LineTypeRec `json:"Row"`
}
type LineTypeRec struct {
	LineType string `json:"LineType"`
}

并修改了我的代码:

var data []entities.LineTypeRec
	for rows.Next() {
		var lineType string
		rows.Scan(&lineType)
		data = append(data, entities.LineTypeRec{lineType})
	}

	result := entities.GetAllLineTypeOfTbPriceScheduleViewModel{
		Integration: entities.IntegrationRec{
			GetAllLineTypeOfTbPriceSchedule: entities.GetAllLineTypeOfTbPriceScheduleRec{
				Database: entities.DatabaseRec{
					Rows: entities.RowRec{
						Row: data,
					},
				},
			},
		},
	}

	return &result, nil

然而编译器仍然在下面这行报错: GetAllLineTypeOfTbPriceSchedule: entities.GetAllLineTypeOfTbPriceScheduleRec{

错误信息是:在类型 entities.IntegrationRec 的结构体字面量中存在未知字段 ‘GetAllLineTypeOfTbPriceSchedule’

有什么提示吗?

要访问内部匿名结构体类型,你需要通过完整路径声明变量。编译器错误是因为你试图将 result.Integration... 用作类型,但 result 是变量名。

以下是两种解决方案:

方案1:使用完整类型声明(推荐)

// 声明变量时直接使用完整类型路径
var row []struct {
    LineType string `json:"LineType"`
}

// 或者使用类型别名提高可读性
type RowType = []struct {
    LineType string `json:"LineType"`
}

var row RowType

方案2:通过临时变量访问

result := entities.GetAllLineTypeOfTbPriceScheduleViewModel{}

// 先获取到Rows,然后访问其Row字段
rows := result.Integration.GetAllLineTypeOfTbPriceSchedule.Database.Rows
row := rows.Row  // row现在是[]struct{LineType string}

方案3:使用类型断言(如果需要从JSON解析)

// 假设data是JSON字节
var result entities.GetAllLineTypeOfTbPriceScheduleViewModel
json.Unmarshal(data, &result)

// 直接访问解析后的数据
for _, r := range result.Integration.GetAllLineTypeOfTbPriceSchedule.Database.Rows.Row {
    fmt.Println(r.LineType)
}

方案4:重构为具名类型(长期维护推荐)

type Row struct {
    LineType string `json:"LineType"`
}

type Rows struct {
    Row []Row `json:"Row"`
}

type Database struct {
    Rows Rows `json:"Rows"`
}

type GetAllLineTypeOfTbPriceSchedule struct {
    Database Database `json:"Database"`
}

type Integration struct {
    GetAllLineTypeOfTbPriceSchedule GetAllLineTypeOfTbPriceSchedule `json:"GetAllLineTypeOfTbPriceSchedule"`
}

type GetAllLineTypeOfTbPriceScheduleViewModel struct {
    Integration Integration `json:"Integration"`
}

// 现在可以这样声明
var row []Row

在Go中,匿名结构体类型是独立的,即使它们结构相同。方案1或方案4是最清晰的做法,特别是当需要单独使用内部结构时。

回到顶部