是否有可能在golang中触发自定义库的编译时错误? [英] Is it possible to trigger compile time error with custom library in golang?

查看:226
本文介绍了是否有可能在golang中触发自定义库的编译时错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有 min()(例如)一个可变参数函数来定义所提供的多个值中的最小值。



如果调用者没有提供任何参数,我想停止编译过程(因为这是调用者中的错误,而不是函数中的错误)。

如何做到这一点?

解决方案

调用具有variadic参数和传递参数的函数是有效的语言规范。所以你不能让它成为编译时错误。然而,你可以修改你的函数的签名,使其具有非可变参数

  func min(first)是一个可变参数,然后在没有参数的情况下调用它会确实是一个编译时错误。 int,rest ... int)int {
m:= first
for _,v:= range rest {
if v < m {
m = v
}
}
返回m
}

这将强制调用者传递至少1个参数,否则这将是编译时错误。 min()函数可以这样调用: 1)
min(1,2)
min(1,2,-3)

但试图在没有任何参数的情况下调用它会导致编译时错误:

  min()//错误:没有足够的参数调用min 

如果您希望调用者传递至少2个参数:

  func min(first,second int,rest ... int)int {
return 0 //在这里实现您的逻辑
}

注意:

如果调用者只是想传递1个参数,上面的例子也更有效率,因为可变参数在背景中用切片实现,如果调用者只传递1个参数,则切片不必被创建,一个 nil 切片将被传递(这可以通过打印 rest == nil 来验证 - 这将是 true )。



潜在的缺点是,如果你有一个切片,你不能只将它传递给函数,但你可以执行以下操作:

  s:= [] int {1,2,-3} 
fmt.Println(min(s [0],s [1:] ...))

传递第一个元素,并切片以传递其余元素并使用 ... 将其作为可变参数的值传递。



试试 Go Playground



如果你不能或不愿意不想修改函数的签名,唯一的选择是在运行时恐慌或退出应用程序,但不可能在编译时失败。


Let's say, I have min() (just for example) a variadic function to define the smallest value from multiple values provided.

If the caller don't provided any parameter, I want to halt compile process (as this would be the bug in the caller, not error in my function).

How to do that?

解决方案

Calling a function which has variadic parameter and passing no arguments is valid by the language spec. So you can't make it a compile-time error.

However, you may modify the signature of your function to have a non-variadic and a variadic parameter, and then calling it with no arguments would indeed be a compile-time error:

func min(first int, rest ...int) int {
    m := first
    for _, v := range rest {
        if v < m {
            m = v
        }
    }
    return m
}

This will force callers to pass at least 1 argument, else it will be a compile-time error. This min() function can be called like this:

min(1)
min(1, 2)
min(1, 2, -3)

But attempting to call it without any arguments results in compile-time error:

min() // Error: not enough arguments in call to min

If you want the callers to pass at least 2 arguments:

func min(first, second int, rest ...int) int {
    return 0 // Implement your logic here
}

Note:

The above example is also more efficient if the caller just wants to pass 1 argument, as variadic parameters are implemented with slices in the background, and if the caller passes only 1 argument, no slice will have to be created, a nil slice will be passed (this can be verified by printing rest == nil – which will be true).

The potential downside is that if you have a slice, you can't just pass it to the function, but you may do the following:

s := []int{1, 2, -3}
fmt.Println(min(s[0], s[1:]...))

Which is passing the first element, and slicing the slice to pass the rest and using ... to pass it as the value for the variadic parameter. Don't forget to check if the slice has at least 1 element, else the above code panics at runtime.

Try the examples on the Go Playground.

If you can't or don't want to modify the signature of your function, your only option is to panic or exit from the app at runtime, but no possibility to fail at compile-time.

这篇关于是否有可能在golang中触发自定义库的编译时错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆