🔍 GetRequireWithVersion
Gets the currently installed version number of a specified package, equivalent to running composer show <package-name> --format=json and parsing the version field.
When to use
Use this when you need to obtain the exact version number of a dependency for version comparison, compatibility checks, or logging. Returns an error when the package is not installed.
Signature
go
func (c *Composer) GetRequireWithVersion(packageName string) (string, error)Parameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | The package name to query the version for, e.g. symfony/console |
Return value
| Return value | Type | Description |
|---|---|---|
| First return value | string | The installed version number of the package, e.g. v5.4.0 |
| Second return value | error | Returned when command execution fails, the package is not installed, or parsing fails |
Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatalf("Initialization failed: %v", err)
}
version, err := comp.GetRequireWithVersion("symfony/console")
if err != nil {
log.Fatalf("Failed to get version: %v", err)
}
fmt.Printf("symfony/console version: %s\n", version)
}Advanced
- To only check whether a package is installed (without the version), use IsPackageInstalled.
- To get all available versions of a package (not the installed version), use
GetPackageVersionsList. - To modify the version constraint in
composer.json, useUpdatePackageVersion.