首页 > 解决方案 > 将 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 `[]`

    }
}

标签: arraysgo

解决方案


您没有获取数组的值,因此您传递给函数的 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 修改函数的返回值更新变量。


推荐阅读