New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: regexp: add functions MustMatch, MustMatchReader, and MustMatchString #58399
Comments
How is |
The purpose of the |
So, just to be clear, the only difference is that your code becomes slightly shorter? You still end up with the same error checking and number of lines, as far as I can tell. Adding new API to just save typing a few characters seems like a bad idea in general to me, unless those savings are very common, like adding |
Yes, you are correct in that the main difference between However, it's important to note that conciseness and readability are still valuable attributes in programming. By making the code easier to follow and understand, MustMatch can improve the overall maintainability and quality of the codebase. Additionally, the ability to write code in a more concise and readable way can increase developer productivity and reduce the likelihood of errors. In summary, the MustMatch function may be a useful addition to the Go programming language even if it only offers a small reduction in code length, as it can improve the overall readability and maintainability of the code. |
The addition of |
I understand your concern that this new function may not necessarily improve code readability and may even add additional complexity to the code. I would like to point out that the primary benefit of the regexp.MustMatch function is to provide a more concise and readable way to match a regular expression, as compared to using regexp.MustCompile(...).Match. In some cases, the use of regexp.MustCompile(...).Match can result in longer and less readable code, especially when used multiple times within a single program. In terms of established patterns, regexp.MustMatch would simply provide an alternative approach that developers can choose to use based on their own preferences and specific use cases. It is important to note that it would not replace the existing regexp.MustCompile(...).Match function, but rather coexist with it as a choice for developers. I hope this clarifies the potential benefits of adding regexp.MustMatch to the Go standard library. |
I believe we can decline this as another variant of #47593 (comment) these shorter functions are simple wrappers for any package that needs it frequently enough to write. |
Proposal: Add functions MustMatch, MustMatchReader, and MustMatchString
Description
The proposed functions
MustMatch
,MustMatchReader
, andMustMatchString
would provide a convenient way to check if a regular expression matches a byte slice, reader, or string, respectively, without having to manually compile the regular expression first. This would improve the readability and simplify the code in certain cases.Syntax
Arguments:
MustMatch
).MustMatchReader
).MustMatchString
).Return value:
Example usage:
Benefits:
Note:
The implementations would internally use
regexp.Compile
and theMatch
,MatchReader
, orMatchString
method, respectively, but hide the details from the user. In case of any error compiling the regular expression, the functions would panic, following the same behaviour asregexp.MustCompile
.The text was updated successfully, but these errors were encountered: