Why Go opt for using * in signature of function that returns a pointer instead of using &?

226 views
Skip to first unread message

Matheus Fassis Corocher

unread,
Mar 5, 2024, 11:33:46 PM3/5/24
to golang-nuts
I was studying about pointers and struct in Golang and one thing that I don't understand is why using * in return of signature of function instead of & if the return of function is an address of variable?

The correct version that works:

func NewVertex(X, Y int) *Vertex {
  ...
  return &Vertex{X, Y}
}

The version in my mind should be correct:

func NewVertex(X, Y int) &Vertex {
  ...
  return &Vertex{X, Y}
}

Ian Lance Taylor

unread,
Mar 5, 2024, 11:50:59 PM3/5/24
to Matheus Fassis Corocher, golang-nuts
It was considered, but following the C programming language was the final choice.

Ian

Kurtis Rader

unread,
Mar 6, 2024, 12:24:50 AM3/6/24
to Matheus Fassis Corocher, golang-nuts
Anyone familiar with C/C++ will be familiar with the current convention of using an asterisk to indicate a pointer to the value. Yes, there is a reasonable argument for using an ampersand but either syntax is essentially arbitrary; thus a coin flip as to which to use. So we might as well use the C convention which a lot of programmers are familiar with rather than using the alternative.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9c60ff1f-6ddb-4136-bfab-642c102a47c7n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Matheus Fassis Corocher

unread,
Mar 6, 2024, 8:55:22 AM3/6/24
to golang-nuts
Thank for your answers, makes a lot of sense.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages