Passing dependency container struct by value or reference

Hi,

I am just wondering which practise below is right way. By the way receiving functions won’t modify the container.

Thanks

type Container struct {
	Version  string
	Shutdown time.Duration

	SomeStructA StructA
	SomeStructB *StructB

	DB     *sql.DB
	SRV    *http.Server
	AWSS3  *s3.S3
	AWSSES *session.Session
}

func main() {
	con := Container{ /* Assume that all fields are populated here */ }

	a(con) // Pass by value
	// OR
	b(&con) // Pass by reference
}

func a(Container)  {}
func b(*Container) {}

I think this article gives you a good insight (When to use pointers in Go. One of my pet peeves is finding places… | by Dylan Meeus | Medium)

1 Like

this links will help you

https://dave.cheney.net/2017/04/29/there-is-no-pass-by-reference-in-go

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