gen_must
is a go tool used to auto-generate Must*
(or must*
) function wrappers by simply marking the function with a comment tag //@gen_must
.
gen_must [-out filename] file_0.go file_1.go ... file_n.go
Given a file decrement.go
with the function:
package decrement
func DecrementUInt(v uint) (uint, error) {
//@gen_must
if v == 0 {
return 0, errors.New("value is zero")
}
return v - 1, nil
}
And running:
gen_must -out musts.gen.go decrement.go
Generates the file musts.gen.go
, with the content:
// Code generated - DO NOT EDIT.
// This file is auto generated by gen_must and any manual changes will be lost.
package decrement
// MustDecrementUInt has the behavior of DecrementUInt, except it panics on error
func MustDecrementUInt(v uint) uint {
var0, err := DecrementUInt(v)
if err != nil {
panic(err)
}
return var0
}
gen_must
can also generate wrappers for private functions and methods.
To customize the name of the generated function with the syntax: //@gen_must: newName
package decrement
func DecrementUInt(v uint) (uint, error) {
//@gen_must: PanicOnFailToDecrementUInt
if v == 0 {
return 0, errors.New("value is zero")
}
return v - 1, nil
}
Results in:
// Code generated - DO NOT EDIT.
// This file is auto generated by gen_must and any manual changes will be lost.
package decrement
// PanicOnFailToDecrementUInt has the behavior of DecrementUInt, except it panics on error
func PanicOnFailToDecrementUInt(v uint) uint {
var0, err := DecrementUInt(v)
if err != nil {
panic(err)
}
return var0
}