DEV Community

Inhere
Inhere

Posted on

gookit/goutil v0.7.2 Released: Enhanced Features & Fixes for an Improved Development Experience

We're excited to announce the release of gookit/goutil v0.7.2! This version brings numerous new features, optimizations, and bug fixes, further enhancing the stability and usability of this Go utility library.
This update covers multiple modules, including math utilities, string processing, filesystem operations, and command-line flag handling, aiming to provide developers with a richer and more efficient toolkit.

🛠️ Key Bug Fixes

This version fixes several key issues to ensure the stable operation of the library:

  • ccolor module: Fixed the issue where the NO_COLOR environment variable was not working correctly during printing, improving the compatibility of color output.
  • cflag module:
    • Fixed a capp runtime error that occurred when parsing global flags.
    • Fixed the issue where the OnAppFlagParsed event was not triggered when no command was input, ensuring the integrity of the event handling logic.

✨ New Feature Highlights

Math Utilities

  • Added the IsInteger function for strictly checking if a value is of an integer type (e.g., int(x), uint(x)).
  • Added StrictInt and StrictUint conversion functions for stricter integer type conversions.
  • Added the FormatBytes function to format byte sizes into human-readable strings (e.g., "1.2 MB"), convenient for log output and UI display.

String Utilities

  • Added the NumVersion function to quickly extract the numeric part from a version string.
  • Added the ReplaceVars function to support quick rendering of strings containing {var} placeholders, simplifying template replacement operations.
  • Added IsUpper and IsLower functions to check if a string is entirely uppercase or lowercase.
  • Added IsUint and IsPositiveNum functions to enhance string numeric validation capabilities.
  • Added BaseConvInt and BaseConvIntByTpl functions to support integer conversion between different bases.
  • Added the ContainsByteOne function to check if a string contains any of the specified bytes.
  • Optimized the MTimeBaseID function, which now supports bases greater than 36.

Filesystem Utilities

  • Added the EnsureDir function to ensure a directory exists (creates it if it doesn't).
  • Added HomeDir/UserHomeDir functions to get the user's home directory path.
  • Added CreateSymlink and IsSymlink functions to support the creation and detection of symbolic links.
  • Added FindAllInParentDirs, FindOneInParentDirs, and FindNameInParentDirs functions to support finding files in parent directories, simplifying the location of project configuration files.

Array/Slice Utilities

  • Added the ToMap function to convert a list (slice) to a map.
  • Added the Map1 function to convert a list to a new list (similar to a map operation).

Map Utilities

  • Added the AppendSMap function for merging string maps.
  • Added the AliasesNames function for handling alias names.

Testing Utilities

  • Added the assert.StrContainsAll assertion function to check if a string contains all specified substrings.
  • Added the testutil.SafeBuffer type, a thread-safe buffer designed for concurrent testing scenarios.
  • Enhanced testutil.EchoServer, which now supports responding with 404, 405, 500, or custom status codes, facilitating the testing of HTTP client error handling logic.

Command-line Utilities

  • The cflag module has a new HelpOnEmptyArgs configuration option to automatically display help information when no arguments are provided, improving user experience.
  • Optimized the rendering of help information.
  • Added support for setting aliases for cflag/capp commands.

Time Utilities

  • Added the FormatDuration function to format time duration into a clock-like format (e.g., "1h 23m 45s"), perfect for displaying performance statistics.

⚡ Performance Optimizations & Refactoring

  • Optimized the common logic of the ToStringWith function to improve conversion performance.
  • Refactored cflag.App into the subpackage capp.App, improving code organization.
  • Optimized the handling options logic in the ToInt64With function to enhance conversion efficiency.
  • Split integer conversion related code into conv2int.go and optimized the performance of all integer conversion related methods in mathutil.
  • Updated the VersionCompare function logic, fixing some comparison errors and improving the accuracy of version comparison.
  • The VarReplacer in textutil now supports parsing variable names as environment variables, enhancing dynamic configuration capabilities.

🌐 Platform Compatibility & Development Workflow

  • Added support for FreeBSD/Unix systems, expanding the platform coverage of the sysutil package.
  • Added comprehensive GitHub Copilot instructions for gookit/goutil, optimizing the AI-assisted development workflow.
  • Upgraded the github/codeql-action dependency from version 3 to 4, enhancing the security and capabilities of code analysis.

📝 Other Updates

  • Added a ShowLen option for dump output, allowing control over whether to display length information.
  • Updated the README generation CLI and related documentation, improving the project documentation generation process and templates.
  • Fixed unit test errors and unified the code style.
  • Updated some comments and code styles.

📦 How to Update

You can use the following command to get the latest version:

go get github.com/gookit/goutil@v0.7.2
Enter fullscreen mode Exit fullscreen mode

Migration & Compatibility Notes

  • cflag -> cflag/capp Subpackage Refactoring: cflag.App has been refactored to capp.App. If your code directly references cflag.App, please adjust:
    • Old: import "github.com/gookit/goutil/cflag"
    • New: import "github.com/gookit/goutil/capp"
    • And switch related types/calls to the App in the capp package.

TIP: This is a potential breaking change in this release, please check and update your references before upgrading.

  • VersionCompare and some string/version-related logic have been optimized. If you rely on the edge-case behavior of the old logic (such as equality comparison or sorting), please run relevant unit tests to verify after upgrading.

Example Snippets

Using mathutil.IsInteger / StrictInt

import "github.com/gookit/goutil/mathutil"
import "github.com/gookit/goutil/testutil/assert"

val := 123
if mathutil.IsInteger(val) {
    // true
}
i64, ok := mathutil.StrictInt("42") // Does not allow string to integer conversion
assert.False(t, ok)
Enter fullscreen mode Exit fullscreen mode

Using strutil.ReplaceVars

import "github.com/gookit/goutil/strutil"

tpl := "Hello, {name}! Today is {day}."
out := strutil.ReplaceVars(tpl, map[string]string{
    "name": "Gopher",
    "day":  "Wednesday",
})
// Output: "Hello, Gopher! Today is Wednesday."
Enter fullscreen mode Exit fullscreen mode

Using fsutil.EnsureDir

import "github.com/gookit/goutil/fsutil"

if err := fsutil.EnsureDir("/tmp/myapp/logs"); err != nil {
    // Handle error
}
Enter fullscreen mode Exit fullscreen mode

🤝 Contributing

We thank all contributors for their efforts in this release! If you find any issues or have feature suggestions, please feel free to open an issue at GitHub Issues.

📖 More Information

Enjoy the new features and improvements in v0.7.2

Top comments (0)