» Make grep CLI App in Go » 2. Development » 2.6 Build

Build

To create a binary, use:

go build -o gorep

It produces a file named gorep in your project directory.

To install the binary to the system's Go bin path, you can use this:

go install

If you want to specify a different name for the installed binary, you can use the //go:install comment directive in your Go source file.

main.go:

//go:install gorep
package main

...

After installation, you can do this:

gorep -n result main.go

You should be able to see something similar to this:

52: var result grep.MatchResult
56: result, err = grep.GrepRecursive(pattern, filePath, options)
61: result, err = grep.Grep(pattern, filePath, options)
68: fmt.Println(grep.GrepCount(result))
70: printResult(result, *lineNumberFlag)
74: func printResult(result grep.MatchResult, lineNumberOption bool) {
76: fileCount := len(result)
78: for filePath, items := range result {

At this point, you have a bunch of commands (run, test, lint and etc) to remember. Just add a Makefile to make it easier.

Makefile:

.PHONY: run test clean

run:
	go run main.go -r -n result .

test:
	go test -v ./...

install:
	go install

build:
	go build -o gorep

clean:
	rm -rf gorep

Then you can easily do these operations:

make run
make test
make build
make install