go - 如何对数组中的所有数字进行平方?戈朗

标签 go

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

谁能告诉我平方的正确方法? 问题是square(type[4]int)的direct无效

最佳答案

你可能想要这样的东西:

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

关于go - 如何对数组中的所有数字进行平方?戈朗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27107674/

相关文章:

go - 内存有效的方式

go - Go中的 '*'和 '&'符号是什么意思?

go - FileInfo.IsDir() 未检测到目录

angular - 为 Nativescript 移动应用程序在本地开发 Golang 后端

tree - 使用go语言提取目录层次结构

multithreading - 为什么基于 channel 的 Lock block ?

mongodb - MongoDB 中的 Golang 嵌套对象

go - 这是什么意思?

xml - 编码 XML Go XMLName + xmlns

mysql - 如何确定数据库服务器或嵌入式数据库是否合适