I am writing golang program and use function regexp.MustComplile. but it may be panic at some time you don't know
just like regexp.MustCompile("Expressions John.Smith will cause panic but you don't known").
I want got error during compilation , not at the runtime.
Is there any way to make that report an error during compilation?
thanks for your help
regexp.MustCompile()
is a function that can only run at runtime, so you can't make a compile-time error using it. The most you can do is call it from a package init()
function (or use it in a global variable initialization), so the error will be detected early, at the start of your program.
Generally, you can't validate strings (semantically) at compile time (whether they fulfill your custom, arbitrary rules).
Best practice is to write unit tests for these things, unit tests which should always run as part of your CI process, so these things surface before your code goes to production.