Arrays 将int[]传递给函数,但函数返回空数组
我将字符串数组和空整数数组传递到函数中。函数的作用是将字符串数组的每个元素转换为整数,并将其存储到整数数组中。当我从函数本身打印整数数组时,一切正常。但是,当我试图打印函数外部的整数数组时,它会打印一个空数组Arrays 将int[]传递给函数,但函数返回空数组,arrays,go,Arrays,Go,我将字符串数组和空整数数组传递到函数中。函数的作用是将字符串数组的每个元素转换为整数,并将其存储到整数数组中。当我从函数本身打印整数数组时,一切正常。但是,当我试图打印函数外部的整数数组时,它会打印一个空数组 employeeDataInt是整数数组,employeeDataString是字符串数组 如果这是一个愚蠢的问题,我道歉,但我是新手。谢谢 package main import ( "bufio" "fmt" "log" "os" "strco
employeeDataInt
是整数数组,employeeDataString
是字符串数组
如果这是一个愚蠢的问题,我道歉,但我是新手。谢谢
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func strToInt(employeeDataString []string, emplyoeeDataInt []int) []int {
for _, i := range employeeDataString[2:] {
j, err := strconv.Atoi(i)
if err != nil {
panic(err)
}
employeeDataInt = append(employeeDataInt, j)
fmt.Println(employeeDataInt) //this prints out the appropriate array
}
return employeeDataInt
}
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter file name: ")
fileName, err := reader.ReadString('\n')
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
fileName = strings.TrimSuffix(fileName, "\n")
file, err := os.Open(fileName)
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var employeeLine []string
for scanner.Scan() {
employeeLine = append(employeeLine, scanner.Text())
}
file.Close()
var employeeDataString = []int{}
for _, employee := range employeeLine {
employeeDataString := strings.Split(employee, " ")
strToInt(employeeDataString, employeeDataInt)
fmt.Println(playerData2) //this is outputting just `[]`
}
}
您没有获取数组的值,因此传递到函数中的切片可能会正确更新,也可能不会正确更新
strToInt(employeeDataString, employeeDataInt)
// should be
employeeDataInt = strToInt(employeeDataString, employeeDataInt)
而且,在使用时,您永远不会分配playerData2
。所以fmt.Println(playerData2)
将始终是[]
但除此之外,您在这里使用数组/切片时还存在一些微妙的问题:
首先是以下两者之间的区别:
Go不允许您直接使用阵列。
除非它们有固定的长度([3]int{}
或[]int{1,2,3]
),否则实际上看的不是数组,而是切片([]int
)
切片只是一个指向数组的指针(以及它的容量和一些其他信息),它本质上允许Go安全地处理数组,因为您永远不会增长现有数组(数组的大小在初始化时是固定的)。因此,您永远不能附加到数组
Go会给你一种附加到一个数组的错觉,那就是有一个大于所需的底层数组,而切片
控制对该数组的访问。因此,如果底层数组的容量为5,并且你已经在其中存储了3项,那么你可以执行2个附加
操作,而不必分配新数组和将现有数组元素复制到新内存位置
因此,当您传递[]int
时,实际上是在传递数组指针(按值)
这将导致代码中的下一个问题:使用append
。
如上所述,append
获取一个切片,查看底层数组以及实际剩余的空间,然后向其添加或分配一个新数组。如果分配了一个新数组,则append
返回一个指向新数组的新切片
所谓:
foo := []{1,2,3}
append(foo, 4)
append(foo, 5)
append(foo, 6)
fmt.Print(foo)
// => might return 1,2,3,4,5
您必须始终获取append
的返回值,否则您仍有可能引用未追加新项目的“旧”切片
因此,增长切片或处理切片的正确方法是记住:,因此始终使用切片修改函数的返回值更新变量。代码中存在一些问题:
- 您正在丢弃
stroint
的返回值
- 您试图在
main
中使用employeeDataInt
,但它在那里没有定义(这应该会导致编译错误,而不是运行时问题)
- 您在
main
(for
循环的内部和外部)的两个不同范围内,使用两种不同的类型([]string
和[]int
)声明了employeeDataString
两次。外部范围的变量未使用,因此也应该会导致编译错误
您正在打印从未定义或使用过的playerData2
——同样,这应该会导致编译器错误,而不是错误行为
考虑到代码中存在编译错误,或者您的帖子中缺少一些关键代码,或者您没有注意到/提到编译错误
main中的正确代码为:
var employeeDataInt []int // Seems like you just have the wrong variable name here
for _, employee := range employeeLine {
employeeDataString := strings.Split(employee, " ")
// You're missing the assignment here
employeeDataInt = strToInt(employeeDataString, employeeDataInt)
fmt.Println(employeeDataInt) // This was referencing the wrong variable
}
将返回值从strotint
分配到employeeDataInt
。否则,更新的切片将被忽略。您的意思是,除了将int[]
作为返回值之外,还将employeeDataInt[]
作为返回值吗?因为我之前尝试过,它给了我缺少的函数体
和语法错误:意外[在顶级声明之后
errors@ThunderCatt函数返回一个值,但调用函数忽略它。使用employeeDataInt=strToInt(employeeDataString,employeeDataInt)进行修复)
。另外,用一个更新问题。程序中没有声明或设置变量playerData2
。重新阅读post-strotint
正确地使用了append
的返回值。问题是调用它的地方是丢弃strott
的返回值。我知道:)但它很重要y同样的问题(附加就是一个例子)。[]INT/EXC>是一个按引用传递的切片,它不被传递给<代码>附加< <代码> >代码> Sttoint 在GO中没有传递引用。它在语言中是如此的明确,您不会认为它是一个传统的引用:指针:但是问题是它没有被指针或引用传递。它是P。按值assed。它们共享相同的基础数组,直到基础数组被append
更改。