在使用 Golang 时,出现了 “在函数末尾缺少返回” 的错误。

huangapple go评论75阅读模式
英文:

Getting missing return at end of function while using return Golang

问题

当我运行我的代码时,输出告诉我缺少一个返回语句,即使我已经使用了一个。

这是我的代码:

func uitchecken(product string, balance float64, voltarief float64, instaptarief float64) float64 {
    
    // 这部分代码将字符串转换为float64

    if s, err := strconv.ParseFloat(product, 64); err == nil {
        result1 := s

        result := balance - (result1 * voltarief) + instaptarief
        return result
    }
}

这段代码的思路是获取一个字符串和三个包含在其中的float64,然后进行一些数学运算,并在主函数中返回该值。

英文:

When i'm running my code, the output tells me that i'm missing a return statement even when i'm using one.

This is my code:

func uitchecken(product string, balance float64, voltarief float64, instaptarief float64) float64 {
    
// This part of the code makes a string into a float64

	if s, err := strconv.ParseFloat(product, 64); err == nil {
		result1 := s

		result := balance - (result1 * voltarief) + instaptarief
		return result
	}
}

The idea of this code is to get 1 string and 3 float64 in it then do a little bit af math and returns the value in the main function.

答案1

得分: 3

如果 err != nil,你的函数实际上什么都不返回。

我会说,按照Go语言的惯例来看,这段代码是一个反模式(尽管它可能偶尔有用),因为在Go语言中,良好的风格是将主要的程序流程放在“主线上”,就像这样:

val, err := doSomething()
if err != nil {
  // 处理错误
}

// 处理 `val` 的“正常”流程在主线上。

在你的特定情况下,我看不出为什么解析 product 可能会失败。

如果你确信它不会失败,可以使用代码中的 panic,就像这样:

func mustParseFloat(s string) float64 {
  s, err := strconv.ParseFloat(product, 64)
  if err != nil {
    panic(err)
  }
  return s
}

func uitchecken(product string, balance float64, voltarief float64, instaptarief float64) float64 {
  result1 := mustParseFloat(product)
  return balance - (result1 * voltarief) + instaptarief
}

然而,根据你的变量名,我感觉到了你原始示例中的“代码异味”:在变量 product 中包含一个需要解析的字符串,而参与同一计算的其他变量都是 float64 类型;感觉上,最好在接收到用户(或客户端)输入的地方尽早解析该字符串,并检查和报告可能的解析错误给用户/客户端。
换句话说,首先对用户的输入进行验证,然后在已知为有效数据的基础上进行操作,这是一种良好的工程实践。

英文:

If err != nil, your function literally returns nothing.

I would say, the code as presented in an ani-pattern in Go (though, it may indeed occasionally be useful) because a good style in Go is to have the main program flow to be "on the main line", like in

val, err := doSomething()
if err != nil {
  // Handle the error out of the way
}

// "Normal" flow dealing with `val` is on the main line.

In your particular case, I cannot see why parsing of product may not fail.

If you are sure it cannot, make the code panic, like with:

func mustParseFloat(s string) float64 {
  s, err := strconv.ParseFloat(product, 64)
  if err != nil {
    panic(err)
  }
  return s
}

func uitchecken(product string, balance float64, voltarief float64, instaptarief float64) float64 {
  result1 := mustParseFloat(product)
  return balance - (result1 * voltarief) + instaptarief
}

Still, given the names of your variables, I sense the "code smell" with your original example: it's strange to see the variable product contain a string which is needed to be parsed while the rest of the variables participating in the same calculation are float64s; it feels like you'd better try to parse that string somewhere way closer to the point it was received from the user (or the client)—with the possible parsing error checked and reported to that user/client right there.
In other words, it's a good engineering practice to first validate the user's input and then operate on the known-to-be-valid data.

答案2

得分: 0

返回 if 后的内容

func uitchecken(product string, balance float64, voltarief float64, instaptarief float64) float64 {

// 这部分代码将字符串转换为 float64

if s, err := strconv.ParseFloat(product, 64); err == nil {
    result1 := s

    result := balance - (result1 * voltarief) + instaptarief
    return result
}
return something <<<<<<<<<<

}


<details>
<summary>英文:</summary>

Return after if

func uitchecken(product string, balance float64, voltarief float64, instaptarief float64) float64 {

// This part of the code makes a string into a float64

if s, err := strconv.ParseFloat(product, 64); err == nil {
    result1 := s

    result := balance - (result1 * voltarief) + instaptarief
    return result
}
return something &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;

}


</details>



huangapple
  • 本文由 发表于 2021年12月16日 03:19:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/70369217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定