go - 为什么我在此代码中的函数末尾缺少返回值?

标签 go

func getKeyNameFromDeploymentAndSubnet(subnetType SubnetType, deploymentType DeploymentType, keyNameMap map[SubnetType]string) string {
    if (deploymentType == NoDeployment || deploymentType == PDBAWindows || deploymentType == AgentDeployment) {
        return keyNameMap[subnetType]
    }
    else if (deploymentType == AnsibleDeployment) {
        return "bar"
    }
    return "foo"
}

在第一个 if 语句中,我得到一个函数错误结尾缺少返回值的错误。如果删除 else if 语句,我不会收到此错误。我哪里错了?

最佳答案

您收到此错误是因为 else 语句必须与第一个条件的结束 } 在同一行。

func getKeyNameFromDeploymentAndSubnet(subnetType SubnetType, deploymentType DeploymentType, keyNameMap map[SubnetType]string) string {
    if deploymentType == NoDeployment || deploymentType == PDBAWindows || deploymentType == AgentDeployment {
        return keyNameMap[subnetType]
    } else if deploymentType == AnsibleDeployment {
        return "bar"
    }
    return "foo"
}

关于go - 为什么我在此代码中的函数末尾缺少返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45071362/

相关文章:

go - 如何为一元 rpc 定义响应 header

CGO : use typedef struct in preamble

戈朗 : Preferred way to tag a struct with aribtrary data

go - 如何从代码中设置 glog 的日志目录

select - Golang - 使用两个 Reader 返回行作为 select 语句

windows - 在 Go 和 Windows API 中处理未确定大小的数组

if-statement - 其他人真的是代码味道吗?编写 Go 条件的惯用方式

go - 在 Go 中,即使 []byte 是按值传递给方法的,但还是会修改原始值?

go - 如何将数据库中的点坐标转换为GoLang中的点结构

go - 如何将带有映射属性的基于结构的类型保存到 mongodb 中