arrays - 将 int[] 传递给函数但函数返回空数组
问题描述
我将一个字符串数组和一个空整数数组传递给一个函数。该函数的重点是将字符串数组的每个元素转换为整数并将其存储到整数数组中。当我从函数本身打印整数数组时,一切都很好。但是,当我尝试在函数外部打印整数数组时,它会打印一个空数组。
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 `[]`
}
}
解决方案
您没有获取数组的值,因此您传递给函数的 Slice 可能会或可能不会正确更新。
strToInt(employeeDataString, employeeDataInt)
// should be
employeeDataInt = strToInt(employeeDataString, employeeDataInt)
在此过程中,您永远不会分配playerData2
. fmt.Println(playerData2)
将永远如此[]
。
但除此之外,您在这里使用数组/切片还有一些微妙的问题:
Slices
首先和Arrays
之间的区别:
Go 不允许您直接使用数组。除非它们具有固定长度 ([3]int{}
或[]int{1,2,3]
),否则您实际上并不是在查看数组,而是查看Slice
( []int
)。
slice 只是一个指向数组的指针(连同它的容量和其他一些信息),它本质上允许 Go 安全地处理数组,因为您永远不会增长现有数组(数组的大小在初始化时是固定的)。所以你永远不能追加到一个数组。
Go 所做的给你附加到数组的错觉是拥有一个大于所需的底层数组,并且Slice
控制对该数组的访问。因此,如果底层数组的容量为 5 并且您已经在其中存储了 3 个项目,则您可以执行 2 次append
操作,而无需分配新数组并将现有数组元素复制到新的内存位置。
因此,当您传递 a 时,[]int
您实际上是在传递一个数组指针(按值)。
这导致代码中的下一个问题:使用append
. 如上所述,append
获取一个 Slice,查看底层数组以及实际剩余多少空间,然后添加到它或分配一个新数组。如果分配了一个新数组,则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
否则您仍然冒着引用未附加新项目的“旧”切片的风险。
因此,增长 Slice 或使用 Slice 通常的正确方法是记住: Slice是按值传递的,因此请始终使用 Slice 修改函数的返回值更新变量。
推荐阅读
- asp.net - asp.net 网站与 Azure 中 Kubernetes/docker 微服务之间的通信
- c# - 如何在 C# 中验证 Json Schema 中的额外键并抛出错误
- graph - 是否可以在 Datadog 中创建 SLO 的时间序列图?
- c - 在 int main、未初始化的内存、未初始化的局部变量中使用函数的问题
- django - Django - 我应该索引模型中的哪些字段或字段以加快以下查询?
- sql-server - Django - 某些 Py 模块在 IIS 中不起作用
- c++ - 指向对象指针的指针如何工作?
- java - 如何捕获出现在 ImapMailReceiver 中的 AuthenticationFailedException
- vba - 更改 PowerPoint 图表中的数据标签格式
- angular - 更新表格