2017-07-13 5 views
0
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ステートメントを削除してもこのエラーは発生しません。どこが間違っていますか?このコードで、関数の最後に返されていない戻り値が返されるのはなぜですか?

ありがとうございます!

+0

常にgofmtを使用してください。それはこの問題を解決しました。 – Flimzy

答えて

3

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" 
} 
1

あなたが最初ifでreturnステートメントを持っているので、あなただけのelse文が低下することがあります。第2のifは、第1の条件が満たされていても、とにかく到達することはありません。

func getKeyNameFromDeploymentAndSubnet(subnetType SubnetType, deploymentType DeploymentType, keyNameMap map[SubnetType]string) string { 
    if (deploymentType == NoDeployment || deploymentType == PDBAWindows || deploymentType == AgentDeployment) { 
     return keyNameMap[subnetType] 
    } 
    if (deploymentType == AnsibleDeployment) { 
     return "bar" 
    } 
    return "foo" 
} 
関連する問題