Bundle merges multiple Go source files into single one.
go get -u github.com/bbrodriges/bundle
As a separate command:
$ bundle --help
Usage: bundle -p <package_name> [-d] pattern...
Available options:
-d Delete source files after bundling
-p string
Package name to be written into result file. Required
$ bundle -d -p main *_gen.go > bundle.go # bundle all autogenerated files in current folder
$ bundle -d -p main -o bundle.go user.msgp.go user.str.go # bundle only specific files
As a go generate
flag:
package types
//go:generate msgp -o=user.msgp.go -tests=false -io=false
//go:generate stringer -type=User -output=user.str.go -linecomment
//msgp:shim UserState as:string using:(UserState).String/UserStateFromString
//go:generate bundle -d -p=types -o=user_gen.go user.msgp.go user.str.go
type User struct {
Username string `msg:"username"`
Email string `msg:"email"`
State UserState `msg:"state"`
}
type UserState int
const (
Unknown UserState = iota -1 // UNKNOWN
Inactive // INACTIVE
Active // ACTIVE
Banned // BANNED
)
func UserStateFromString(s string) UserState {
switch s {
case "INACTIVE":
return Inactive
case "ACTIVE":
return Active
case "BANNED":
return Banned
}
return Unknown
}
The above code upon calling go generate .
will:
- create files called
user.msgp.go
anduser.str.go
with containing autogenerated code - bundle these two files into single
user_gen.go
and delete source filesuser.msgp.go
anduser.str.go
- bundle cannot handle definition conflicts (e.g. structs, functions, constants of the same names)
- bundle cannot handle import cycle conflicts
All it does is collects and strips out import declarations from every given source files AST and concats them into single file with helping comments.