Declare a var of inner type struct

Hi. I received from a system in .NET and I am migrating it to GO.
They asked me that I should maintain the same data structure.
I did the conversion and got

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"`
}

Now I want to create a row type slide and declare like this

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

However, Go compiler marks an error in the first point. It seems that it would take as a package definition … :frowning:

Is there another way to gain access to the internal structure without declaring it as an external structure?

TIA,
Yamil

2 Likes

No, you need to declare those as actual named types.

3 Likes

Yes, Jakob. Thanks a lot!!!

2 Likes

Just another question. I did break the mega-struct into various smaller

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"`
}

And change my code to

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

However the compiler still complaints in line
GetAllLineTypeOfTbPriceSchedule: entities.GetAllLineTypeOfTbPriceScheduleRec{

unknown field ‘GetAllLineTypeOfTbPriceSchedule’ in struct literal of type entities.IntegrationRec

Any hint ?

2 Likes

I solved it !!!
It seems that ‘GetAllLineTypeOfTbPriceSchedule’ is too long and Go compiler uses “n” first characters for vars so it could be get confused with another identifiers in the file.
So I learnt, be simple not so verbose…!.. :slight_smile:

2 Likes

Go does not do that, but it’s a long identifier that’s easy to misspell, or perhaps the file wasn’t saved correctly. :slight_smile:

2 Likes

OK. I re-check and that’s it . A typo!!!
Thanks a lot!!!

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.