将数组/切片中的项目值进行转换。

huangapple go评论91阅读模式
英文:

transform item value in array/slice

问题

我想创建一个新的数组,其中每个值都是原始数组中对应值的两倍,我的代码有什么问题?

func main() {
  myArray := []string{"1", "2", "3"}
  var newArray = []string

  for i, arr := range myArray {
    newArray = newArray[i] * 2
  }

  fmt.Println(newArray)
}

你的代码有几个问题:

  1. 在声明 newArray 变量时,你应该使用 []string{} 来初始化一个空的字符串数组。
  2. 在循环中,你应该使用 append() 函数将新的元素添加到 newArray 中,而不是直接赋值给索引位置。
  3. 在乘以2之前,你需要将字符串转换为整数类型。

下面是修复后的代码:

import (
  "fmt"
  "strconv"
)

func main() {
  myArray := []string{"1", "2", "3"}
  var newArray = []string{}

  for _, arr := range myArray {
    num, _ := strconv.Atoi(arr)
    newNum := strconv.Itoa(num * 2)
    newArray = append(newArray, newNum)
  }

  fmt.Println(newArray)
}

这样,你就可以得到一个新的数组,其中每个值都是原始数组中对应值的两倍。

英文:

I want to create a new array where each value is 2 times the corresponding value in the original array, what's wrong with my code?

func main() {
  myArray := []string{"1", "2", "3"}
  var newArray = []string

  for i, arr := range myArray {
    newArray = newArray[i] * 2
  }

  fmt.Println(newArray)
}

答案1

得分: 3

  1. 使用整数切片而不是字符串切片。例如,[]string{"1", "2", "3"} -> []int{1, 2, 3}
  2. 类型不是值。例如,var newArray = []string 是一个无效的语句。要初始化一个空切片,省略=运算符,或使用复合类型字面量,或者在这种情况下更好地使用 make 函数并指定 myArray 的长度。
  3. 未使用的块变量在 Go 中是非法的。例如,在 for i, arr := myArray { 中的 arr 在循环中未被使用,因此会导致编译时错误。
  4. 不能将字符串与整数相乘。例如,如果 newArray 的类型是 []string,则 newArray[i] * 2 是非法的,会导致编译时错误。
  5. 不能更改变量的类型。例如,如果 newArray 的类型是 []int,则语句 newArray = newArray[i] * 2 是非法的,因为 newArray[i] * 2 的结果是 int 类型,而不能将 int 赋值给类型为 []int 的变量。
func main() {
	myArray := []int{1, 2, 3}
    newArray := make([]int, len(myArray))

	for i := range myArray {
		newArray[i] = myArray[i] * 2
	}

	fmt.Println(newArray)
}

链接:https://play.golang.org/p/Tc0-51GiAhS

英文:
  1. Use an integer slice instead of a string slice. i.e. []string{"1", "2", "3"} -> []int{1, 2, 3}.
  2. Types are not values. i.e. var newArray = []string is an invalid statement. To initialize an empty slice omit the = operator, or use a composite type literal, or even better in this case, use make with the length of myArray.
  3. Unused block variables are illegal in Go. i.e. The arr in for i, arr := myArray { is not used within the loop and will therefore cause a compile time error.
  4. You cannot multiply a string with an integer. i.e. newArray[i] * 2 where newArray is of type []string is illegal and will cause compile time error.
  5. You cannot change the type of a variable. i.e. newArray = newArray[i] * 2 if newArray is of type []int the statement is illegal because newArray[i] * 2 would evaluate to int and you cannot assign int to a variable of type []int.
func main() {
	myArray := []int{1, 2, 3}
    newArray := make([]int, len(myArray))

	for i := range myArray {
		newArray[i] = myArray[i] * 2
	}

	fmt.Println(newArray)
}

https://play.golang.org/p/Tc0-51GiAhS

答案2

得分: 1

你的语法有几个问题。
你声明数组的方式不正确(第一个声明将myArray声明为切片,第二个声明无效且无法编译),而且你试图对字符串进行乘法运算。
你声明了arr但没有使用它,这也会导致编译错误。

以下代码片段可以实现你想要的整数操作:

myArray := [3]int{1, 2, 3}
newArray := myArray // 将原始数组复制到一个新数组

for i := range myArray { // 只关心索引,不关心值
    newArray[i] *= 2 // 在新数组中将值翻倍
}

fmt.Println(newArray)

另一种方法是在不先复制原始数组的情况下实现:

myArray := [3]int{1, 2, 3}
newArray := [3]int{}

for i := range myArray {
    newArray[i] = myArray[i] * 2
}

fmt.Println(newArray)
英文:

There's quite a few things wrong with your syntax.
You are declaring your arrays wrong ( your first one declares a myArray as a slice, your second one is invalid and won't compile ), and you are trying to multiply strings.
You are declaring arr but not using it which will also give a compile error.

This snippet will do what you want with ints:

myArray := [3]int{1, 2, 3}
newArray := myArray // copies the original array to a new one

for i := range myArray { // don't care about the value, only the index
	newArray[i] *= 2  // double the values in the new array
}

fmt.Println(newArray)

Another alternative without first copying the original array:

myArray := [3]int{1, 2, 3}
newArray := [3]int{}

for i := range myArray {
	newArray[i] = myArray[i] * 2
}

fmt.Println(newArray)

huangapple
  • 本文由 发表于 2021年10月13日 23:59:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/69558679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定