Thursday, July 10, 2025
HomeGolangBase struct as pointer or sub structs as pointers - Getting Assist

Base struct as pointer or sub structs as pointers – Getting Assist


Hello,

I used to be questioning is there may be any distinction (by way of environment friendly reminiscence utilization) between variations beneath. First one is returned as pointer however not its inside objects. Second model makes use of inside objects as pointer however not the bottom one.

Word: Objects can get large, I saved them small for now. FYI – BulkResponse isn’t meant to be mutated in any respect. It is going to simply be returned from one bundle to a different, that’s it.

Thanks

V1

kind Response struct {
	Length   int 
	Success    bool
	Operations []Operation
}

kind Operation struct {
	Create Merchandise
	Replace Merchandise
	Delete Merchandise
	Listing   Merchandise
}

kind Merchandise struct {
	ID     string 
	Errors []Error
}

kind Error struct {
	Code    int
	Cause  string
	Message string
}

func Version1() *BulkResponse {
	res := &BulkResponse{}
	// Construct response ....
	return res
}

V2

kind Response struct {
	Length   int 
	Success    bool
	Operations []*Operation
}

kind Operation struct {
	Create Merchandise
	Replace Merchandise
	Delete Merchandise
	Listing   Merchandise
}

kind Merchandise struct {
	ID     string 
	Errors []*Error
}

kind Error struct {
	Code    int
	Cause  string
	Message string
}

func Version2() BulkResponse {
	res := BulkResponse{}
	// Construct response ....
	return res
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments