Here’s the translation of the Go URL parsing example to Groovy:
Our URL parsing program demonstrates how to parse and extract different components from a URL. Here’s the full source code:
Running our URL parsing program shows all the different pieces that we extracted:
This example demonstrates how to use Groovy’s URI class to parse URLs. The URI class provides methods to access different components of a URL such as scheme, user info, host, port, path, query, and fragment.
Note that unlike Go’s url.Parse function, Groovy’s URI class doesn’t separate the host and port automatically, so we need to handle that manually if needed. Also, for parsing query parameters, we’ve used a Groovy-specific approach to split the query string and convert it into a map.
The program structure and explanations have been maintained as closely as possible to the original, while adapting to Groovy’s syntax and standard library.