Matheus,

Rewire your brain.

The return value or values may be explicitly listed in the "return" 
statement. Each expression must be single-valued and assignable to the 
corresponding element of the function's result type.

The Go Programming Language Specification
https://go.dev/ref/spec#Return_statements

The return &Vertex{X, Y} statement is an implicit assignment to a value of 
type *Vertex.

type Vertex struct {
X, Y int
}

func NewVertex(X, Y int) (v *Vertex) {
// return &Vertex{X, Y}
v = &Vertex{X, Y}
return v
}

func main() {
x, y := 3, 5
var v *Vertex = NewVertex(x, y)
fmt.Println(x, y, *v)
}

https://go.dev/play/p/votlWKXjZH4

peter


On Tuesday, March 5, 2024 at 11:33:46 PM UTC-5 Matheus Fassis Corocher 
wrote:

> 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}
> }
>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/9c3828d4-f462-4830-8e18-c34ae39be6dbn%40googlegroups.com.

Reply via email to