-3
package main

import (
    "fmt"
)

func main() {
    var square int
    box := [4]int{1, -2, 3, 4}

    square = box * *box

    fmt.Println("The square of the first box is", square)
}

Anyone can tell me the correct way to square it? The problem is invalid direct of square(type[4]int)

  • 2
    What is ** supposed to do? – fuz Nov 24 '14 at 15:25
  • ** isn't an operator, so you have box * (*box), but box isn't a pointer so you can't de-reference it. (it still wouldn't make sense if it was a pointer either) – JimB Nov 24 '14 at 18:54
  • @FUZxxl i actually want to square it, sorry for my mistake – Anthony FSS Nov 25 '14 at 2:49
  • thx for correct me @plusmid im a beginner,can you suggest me any ebook or reference of golang? – Anthony FSS Nov 25 '14 at 2:50
  • @AnthonyFSS: Take a look at 'go' tag wiki - Stack Overflow. – peterSO Nov 25 '14 at 4:38
8

You probably want something like this:

package main

import (
  "fmt"
)

func main() {
  box := []int{1, -2, 3, 4}
  square := make([]int, len(box))
  for i, v := range box {
    square[i] = v*v
  }

  fmt.Println("The square of the first box is ", square)
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.