📋 GetInstalledPackageNames
Gets the list of all installed package names, equivalent to running composer show and parsing the output to extract package names.
When to use
Use it when you only need the list of package names without full package information. It is more efficient than getting the full output, and is suitable for batch package existence checks, dependency inventory generation, and similar scenarios.
Signature
go
func (c *Composer) GetInstalledPackageNames() ([]string, error)Parameters
This method takes no parameters.
Return value
| Return value | Type | Description |
|---|---|---|
| First return value | []string | List of installed package names (including direct and transitive dependencies) |
| Second return value | error | Returned when executing composer show 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)
}
packages, err := comp.GetInstalledPackageNames()
if err != nil {
log.Fatalf("failed to get package list: %v", err)
}
fmt.Printf("%d packages installed in total:\n", len(packages))
for _, name := range packages {
fmt.Println(" -", name)
}
}Advanced
- For direct dependencies package names only, use GetDirectDependencyNames.
- To parse any
composer showtext output, use theParsePackageList(output)utility function. - To get package version numbers, use GetRequireWithVersion.