6

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?

Adam Matan
  • 128,757
  • 147
  • 397
  • 562
  • 1
    All your code should be in your GOPATH. Always. Reference: [`How to Write Go Code`](https://golang.org/doc/code.html) – JimB Nov 27 '17 at 17:02
  • 1
    As JimB says. This should be checked out into `/Users/adam/go/src/github.com/elastic/beats`. Then everything will work normally. – Adrian Nov 27 '17 at 17:04
  • Thanks for the comments. I've started my problem clearly and did my homework. Why the downvote? – Adam Matan Nov 28 '17 at 07:47
  • 1
    No need to downvote. Faced exactly the same problem, might be typical for a go newbie. – andig Dec 16 '17 at 17:36

3 Answers3

5

Previous answers to this question are obsolete when developing projects that using Go Modules.

For projects that using Go Modules, one may use the following command to replace an imported library(eg. example.com/imported/module) with a local module(eg. ./local/module):

go mod edit -replace=example.com/imported/module=./local/module

Or by adding the following line into the go.mod file:

replace example.com/imported/module => ./local/module

Reference Docs: https://golang.org/doc/modules/managing-dependencies#unpublished

Tomasen
  • 142
  • 1
  • 3
4

One of the recommended ways to work with other's packages is:

  1. Get the sources of the original package:

    go get github.com/elastic/beats
    

    As a result you will clone project's git repository to the folder

    $GOPATH/src/github.com/elastic/beats
    
  2. Make some fixes, compile code, fix, compile... When you make go install package will be compiled and installed to your system. When you need merge updates from original repository you can git pull them.

Everything is OK. What's next? How to share your work with others?

  1. Fork project on github, suppose it will be github.com/username/beats

  2. Add this fork as another remote mycopy (or any other name you like) to your local repository

    git remote add mycopy git://github.com/username/beats.git
    
  3. When all is done you can push updated sources to your repo on github

    git push mycopy
    

    and then open a pull-request to original sources. This way you can share your work with others. And keep your changes in sync with mainstream.

Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
2

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.

Adrian
  • 42,911
  • 6
  • 107
  • 99