aliaser
is a Go package designed to streamline the process of generating
aliases for Go packages.
In Go projects, naming conflicts between imported packages and your own code can lead to verbose and cumbersome code where you have to frequently alias imported package names.
aliaser solves this problem by automatically generating Go code that provides aliases for all the exported constants, variables, functions, and types in an external package. This allows you to seamlessly integrate and extend external packages without cluttering your code with manual aliases.
You need a working Go environment.
go get github.com/marcozac/go-aliaser
To use aliaser in your project, follow these steps:
- Create an Aliaser: Start by creating a new
aliaser.Aliaser
with the target package name and the pattern of the package you want to alias.
import "github.com/marcozac/go-aliaser"
a, err := aliaser.New(&aliaser.Config{TargetPackage: "mypkg", Pattern: "github.com/example/package"})
if err != nil {
// ...
}
- Generate Aliases: With the
Aliaser
created, you can generate the aliases writing them to aio.Writer
or to directly to a file.
if err := a.Generate(io.Discard); err != nil {
// ...
}
if err := a.GenerateFile("mypkg/alias.go"); err != nil {
// ...
}
In addition to the library, aliaser
comes with a CLI tool to simplify
generating aliases directly from the command line.
After installing aliaser CLI with go install
(e.g.
go install github.com/marcozac/go-aliaser/cmd/aliaser@latest
) or by
downloading a release (available soon), you can use the aliaser
command to
generate aliases for a package.
aliaser generate \
--from "github.com/example/package" \
--package "myalias" \
--file "path/to/output/file.go"
For simple, but more detailed examples of how to use the aliaser
library and
CLI, see the examples directory.
Contributions to aliaser are welcome! Whether it's reporting bugs, discussing improvements, or contributing code, all contributions are appreciated.
This project is licensed under the MIT License. See the LICENSE file for details.