Question about pointers as return type to a function

Viewed 39

Refer to the link here : https://go.dev/play/p/mPYw900P1vT

Line 11-13. What does it mean to pass a “pointer” to a struct and return “address” of the struct?

My understanding is that “&” will return the address of the entity. Why are we returning the address when we want the actual entity?

So I have two questions:

  1. What does returning a pointer to MyStruct mean? (line 11: *MyStruct )

  2. Why are we returning the address to the struct when we want the actual value? (line 12 : &MyStruct{} )

I am new to this and its confusing as I see this pattern in all codes. especially while using third party libraries like net/http.

0 Answers
Related