Search code examples
golibraries

Modifying an imported library in Go


My Problem

Elastic Beats is an open source project for log shippers written in Go. It features several log outputs, including console, Elasticsearch and Redis. I would like to add an output of my own - to AWS Kinesis.

I have cloned the repo to ~/github/beats, and tried building it:

$ cd filebeat; go build main.go

However, it failed due to a missing library which is a part of the project:

main.go:6:2: cannot find package "github.com/elastic/beats/filebeat/cmd" in any of:
    /usr/local/go/src/github.com/elastic/beats/filebeat/cmd (from $GOROOT)
    /Users/adam/go/src/github.com/elastic/beats/filebeat/cmd (from $GOPATH)

A directory of the project is dependent on a package from the same repo, but instead of looking one directory up the hierarchy it looks in the GOPATH.

So, go get github.com/elastic/beats/filebeat/cmd fetched the code, and now go build main.go works. Changing the code in my GOPATH is reflected in these builds.

This leaves me with an structural inconvenience. Some of my code is at a working directory, and some of it is at my GOPATH and included by the code in my working directory.

I would like to have all my code in a single directory for various reasons, not the least being keeping everything under version control.

What Have I Tried

Mostly searching for the problem. I am quite new to Go, so I might have missed the correct terminology.

My Question

What is the right way to edit the code of an imported library in Go?


Solution

  • A project working copy should be checked out into $GOPATH/src/package/import/path - for example, this project should be checked out into /Users/adam/go/src/github.com/elastic/beats. With the project in the correct location, the go tooling will be able to operate on it normally; otherwise, it will not be able to resolve imports correctly. See go help gopath for more info.