dustin/go-humanize is a helpful Go package to format number, string and time. Let’s update our Go program as follow.
1 2 3 4 5 6 7 8 91011
fmt.Printf("That file is %s.\n", humanize.Bytes(82854982)) // That file is 83 MB.
fmt.Printf("You're my %s best friend.\n", humanize.Ordinal(193)) // You are my 193rd best friend.
fmt.Printf("You owe $%s.\n", humanize.Comma(6582491)) // You owe $6,582,491.
Now if we try to build it, it will throws the following error.
[[email protected] glide-example]$ go build -o hello
main.go:3:8: cannot find package "github.com/dustin/go-humanize" in any of:
/home/ykyuen/tools/go1.8.4/src/github.com/dustin/go-humanize (from $GOROOT) /home/ykyuen/go/src/github.com/dustin/go-humanize (from $GOPATH)
This is because we haven’t downloaded the dustin/go-humanize. Without using Glide, we could solve this build error by running the go get command.
go get github.com/dustin/go-humanize
But in this case, anyone who checkouts the project has no idea what packages are needed in order to build the program. If you have already run the go get command, delete the package before proceeding.
The Glide way
Create the glide.yaml
The glide.yaml could be generated by the glide create command. It will scan the Go src files and include all the dependencies needed. Here is the output of this example.
1 2 3 4 5 6 7 8 9101112131415161718
[[email protected] glide-example]$ glide create
[INFO] Generating a YAML configuration file and guessing the dependencies
[INFO] Attempting to import from other package managers (use --skip-import to skip)[INFO] Scanning code to look for dependencies
[INFO] --> Found reference to github.com/dustin/go-humanize
[INFO] Writing configuration file (glide.yaml)[INFO] Would you like Glide to help you find ways to improve your glide.yaml configuration?
[INFO] If you want to revisit this step you can use the config-wizard command at any time.
[INFO] Yes (Y) or No (N)?
[INFO] Looking for dependencies to make suggestions on
[INFO] --> Scanning for dependencies not using version ranges
[INFO] --> Scanning for dependencies using commit ids
[INFO] Gathering information on each dependency
[INFO] --> This may take a moment. Especially on a codebase with many dependencies
[INFO] --> Gathering release information for dependencies
[INFO] --> Looking for dependency imports where versions are commit ids
[INFO] No proposed changes found. Have a nice day.
This will generated the glide.lock files and the vendor folder which contains the dustin/go-humanize package. The glide.lock file contains the version of the package. It could be a tag or a git commit hash.