Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Difference between the types of accessing pointer values in golang

Consider the below example

type Employee struct {
    Firstname string
    // other fields
}

func (e *Employee) SetName(name string) {
   e.Firstname = name // type 1
   (*e).firstName = name // type 2
}

What is the difference between type 1 and type 2 ways of accessing properties here? When should we use one over the other?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Type 1 is a shorthand for type 2. Use the shorthand notation.

Here’s the quote from the specification:

if the type of x is a defined pointer type and (*x).f is a valid selector expression denoting a field (but not a method), x.f is shorthand for (*x).f.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading