在Go语言中使用切片和数组时出现错误。

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

Using slices and Arrays in go error

问题

我正在学习Go语言,我习惯使用Java,所以在编写代码时遇到了一些错误,这些错误在我看来似乎不是问题。以下是我的代码:

package main
import(
	"fmt"
)

func main(){
	f:= [5]int{1,2,3,4,5}
	h:= [5]int{6,7,8,9,10}

	fmt.Println(reverseReverse(f,h))

}
func reverseReverse(first []int, second []int) ([]int, []int){
	//创建临时数组以保存遍历后的数组,然后进行交换
	var tempArr1 []int
	var tempArr2 []int
	//count用于在For循环中正确顺序地计数tempArrays
	var count = 0
	//遍历第一个数组,并将从末尾开始的值设置为temp数组中的值
	//temp数组从左到右递增
	for i :=len(first)-1; i>=0;i--{
		tempArr1[count] = first[i]
		count++
	}
	count =0
	//同第一个循环,只是针对第二个数组
	for i :=len(second)-1; i>=0;i--{
		tempArr2[count] = second[i]
		count++
	}
	//尝试将参数数组的值替换为temp数组的值
	first=tempArr2
	second = tempArr1
	//返回数组
	return first,second
}

基本上,我正在尝试编写一个方法,该方法接受两个数组并返回它们的反转和交换。

例如:

arr1 = {1,2,3}

arr2 = {6,7,8}

应该返回:

arr1 = {8,7,6}

arr2 = {3,2,1}

我遇到的错误如下:

src\main\goProject.go:35: cannot use first (type [5]int) as type []int
in return argument

src\main\goProject.go:35: cannot use second (type [5]int) as type
[]int in return argument

它说:无法将f (类型为[5]int)用作打印语句中的[]int类型的变量。

我之前遇到过问题,并将我的temp数组更改为切片,但我不明白为什么会出现这个错误。

顺便说一下:我尝试将参数数组的长度替换为...,但也没有成功:

func reverseReverse(first [...]int, second [...]int) ([]int, []int){

这导致与之前相同的错误,只是显示为:

f (type [5]int) as type [...]int

所以我的问题是:为什么会出现这个错误?这是我所有的代码,如果需要更多信息,请提问。

英文:

I am learning go and I am used to using Java so I am running into errors than in my mind don't seem to be a problem. Here is my code:

package main
import(
	"fmt"
)

func main(){
	f:= [5]int{1,2,3,4,5}
	h:= [5]int{6,7,8,9,10}

	fmt.Println(reverseReverse(f,h))

}
func reverseReverse(first []int, second []int) ([]int, []int){
	//creating temp arrays to hold the traversed arrays before swapping.
	var tempArr1 []int
	var tempArr2 []int
	//count is used for counting up the tempArrays in the correct order in the For loops
	var count = 0
	//goes through the first array and sets the values starting from the end equal to the temp array
	//which increases normally from left to right.
	for i :=len(first)-1; i>=0;i--{
		tempArr1[count] = first[i]
		count++
	}
	count =0
	//same as first for loop just on the second array
	for i :=len(second)-1; i>=0;i--{
		tempArr2[count] = second[i]
		count++
	}
	//trying to replace the values of the param arrays to be equal to the temp arrays
	first=tempArr2
	second = tempArr1
	//returning the arrays
	return first,second
}

Basically I am trying to write a method that takes two arrays and returns them reversed and swapped.

Ex:

arr1 = {1,2,3}

arr2 = {6,7,8}

should return:

arr1 = {8,7,6}

arr2 = {3,2,1}

My error that I am getting is as such :

> src\main\goProject.go:35: cannot use first (type [5]int) as type []int
> in return argument
>
> src\main\goProject.go:35: cannot use second (type [5]int) as type
> []int in return argument

It says: Cannot use f (type [5]int) as type []int on the variables in my print statement.

I had problems before and swapped my tempArrays to be slices, but I don't understand why I am getting this error.

Side note: I tried replacing the parameters array lengths to ... with no luck either:

func reverseReverse(first [...]int, second [...]int) ([]int, []int){

This created the same error as before just says:

f (type [5]int) as type [...]int

So my question is: why am I getting this error? This is all the code I have comment any questions for more info if need be.

Here:

Before I changed the temp array to a slice I had this:

var tempArr1 [len(first)]int
var tempArr2 [len(second)]int

I still get the same error as stated before, but the new error is:

src\main\goProject.go:15: non-constant array bound len(first)

src\main\goProject.go:16: non-constant array bound len(second)

And I understand it should be constant, but why is using len() not make it constant?

答案1

得分: 2

一些问题:

  • 在访问元素之前,你必须创建一个长度为5的切片。如果你只是简单地使用 var tempArr1 []int,你会出现错误。
  • 你可以使用切片代替数组,这样你的返回类型将与 []int 匹配。

你可以在这里找到修复方法:
https://play.golang.org/p/5E2hL0796o

编辑:为了让你保持数据类型为数组,只需更改返回类型以匹配。你的函数签名应该是这样的:

func reverseReverse(first [5]int, second [5]int) ([5]int, [5]int)

GoPlay 在这里:
https://play.golang.org/p/_eV3Q0kspQ

回答你的问题,你不能让一个函数接受任意大小的数组。你必须指定长度。在 Go 中,[]int 和 [5]int 有根本的区别。

英文:

Couple of issues:

  • You have to make a slice with a length of 5 before you can access elements in it. You'll panic if you simply do var tempArr1 []int
  • You can use slices instead of arrays, and your return types will match with []int

You'll find your fix here:
https://play.golang.org/p/5E2hL0796o

Edit: to allow you to keep your data types as an array, simply change your return types to match. Your function signature should look like this:

func reverseReverse(first [5]int, second [5]int) ([5]int, [5]int)

GoPlay here:
https://play.golang.org/p/_eV3Q0kspQ

To answer your question, you cannot have a function take in an array of arbitrary size. You would have to specify the length. There is a fundamental difference in Go for []int and [5]int.

huangapple
  • 本文由 发表于 2017年6月15日 03:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/44553318.html
匿名

发表评论

匿名网友

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

确定