-2

How can I set one pointer to multiply function?

package main

import "fmt"

type Cube struct {
    u int
}

func (h *Cube) space() int {
        return h.u * h.u * h.u
}

func main() {
        h := Cube {
                u: 10,
        }
        fmt.Println(h.space())

        h := Cube {
                u: 100,
        }
        fmt.Println(h.space())
}

The first request of println give back 1000, but with the second println it goes wrong telling no new variables on left side of := but I want the pointer to use all same just the u: to 100 change

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
aurelien
  • 404
  • 4
  • 22

1 Answers1

4

:= does two things, it creates a variable and assigns a value to it. You are trying to create a new variable called h in the second line and the compiler is telling you that it would not create a new variable.

Just replace that := with =

GarMan
  • 1,034
  • 7
  • 10