title: How to install Golang package locally that's referenced as a github.com address
body: <p>I have a package that I'm referencing in my Go code as:</p>
  
  <pre><code>github.com/apache/pulsar/pulsar-function-go/pf
  </code></pre>
  
  <p>However, I need to include some <em>local</em> changes to that library when building a docker image for setting up an integration test. </p>
  
  <p>In my Dockerfile, I've setup GOPATH and GOROOT and PATH like this:</p>
  
  <pre><code>ENV GOROOT /usr/local/go
  ENV GOPATH /go
  ENV PATH /go/bin:$PATH
  </code></pre>
  
  <p>Then, in the Dockerfile, I copy the source code into my Docker image and attempt to install the Go module, like this:</p>
  
  <pre><code>COPY target/pulsar-function-go/ /pulsar/dependencies/pulsar-function-go/
  RUN cd /pulsar/dependencies/pulsar-function-go/pf &amp;&amp; go install .
  </code></pre>
  
  <p>However, when I then try to build a file that references <code>github.com/apache/pulsar/pulsar-function-go/pf</code> by running this line:</p>
  
  <p><code>RUN go build -o /pulsar/examples/go-examples/exclamationFunc /pulsar/examples/go-examples/exclamationFunc.go</code></p>
  
  <p>I get this exception message:</p>
  
  <blockquote>
    <p>---> Running in bb3ec43f7fea
    examples/go-examples/exclamationFunc.go:24:2: cannot find package
    "github.com/apache/pulsar/pulsar-function-go/pf" in any of:
            /usr/local/go/src/github.com/apache/pulsar/pulsar-function-go/pf (from
    $GOROOT)
            /go/src/github.com/apache/pulsar/pulsar-function-go/pf (from $GOPATH)</p>
  </blockquote>
  
  <p>Is there a way to allow me to use a locally installed version of that same package without needing to change the reference in the code that I'm trying to build? </p>
  
  <p><strong>EDIT Here's everything else I've tried:</strong></p>
  
  <p>When I run go install in pulsar-function-go, I get:</p>
  
  <pre><code>cannot find module for path .
  </code></pre>
  
  <p>When I run go install in pulsar-function-go/pf, I get:</p>
  
  <pre><code>examples/go-examples/exclamationFunc.go:24:2: cannot find package "github.com/apache/pulsar/pulsar-function-go/pf" in any of:
          /usr/local/go/src/github.com/apache/pulsar/pulsar-function-go/pf (from $GOROOT)
          /go/src/github.com/apache/pulsar/pulsar-function-go/pf (from $GOPATH)
  </code></pre>
  
  <p>When I try running:</p>
  
  <pre><code>RUN go get -t github.com/apache/pulsar/pulsar-function-go/...
  </code></pre>
  
  <p>(although this won’t bring in my local changes, it was just for testing)
  I get:</p>
  
  <pre><code># github.com/apache/pulsar/pulsar-function-go/examples/test
  /go/src/github.com/apache/pulsar/pulsar-function-go/examples/test/producer.go:30:6: main redeclared in this block
          previous declaration at /go/src/github.com/apache/pulsar/pulsar-function-go/examples/test/consumer.go:30:6
  # github.com/apache/pulsar/pulsar-function-go/examples
  /go/src/github.com/apache/pulsar/pulsar-function-go/examples/hello.go:32:6: main redeclared in this block
          previous declaration at /go/src/github.com/apache/pulsar/pulsar-function-go/examples/contextFunc.go:36:6
  /go/src/github.com/apache/pulsar/pulsar-function-go/examples/inputFunc.go:34:6: main redeclared in this block
          previous declaration at /go/src/github.com/apache/pulsar/pulsar-function-go/examples/hello.go:32:6
  /go/src/github.com/apache/pulsar/pulsar-function-go/examples/outputFunc.go:33:6: main redeclared in this block
          previous declaration at /go/src/github.com/apache/pulsar/pulsar-function-go/examples/inputFunc.go:34:6
  </code></pre>
  
  <p>When I try running:</p>
  
  <pre><code>RUN go get -t github.com/apache/pulsar/pulsar-function-go
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>package github.com/apache/pulsar/pulsar-function-go: no Go files in /go/src/github.com/apache/pulsar/pulsar-function-go
  </code></pre>
  
  <p>If I try running this to first get some of the dependencies:</p>
  
  <pre><code>RUN go get -t github.com/apache/pulsar/pulsar-function-go/pf
  </code></pre>
  
  <p>it succeeds. If I then follow that in my Dockerfile with:</p>
  
  <pre><code># after using Maven plugin to copy files from the pulsar-function-go source directory to target/pulsar-function-go
  COPY target/pulsar-function-go/ /go/src/github.com/apache/pulsar/pulsar-function-go
  RUN cd /go/src/github.com/apache/pulsar/pulsar-function-go/pf &amp;&amp; go install
  </code></pre>
  
  <p>to replace the source code for pulsar-function-go in the docker file with my local code, then when I try to build the binary, like this:</p>
  
  <pre><code>RUN go build -o /pulsar/examples/go-examples/exclamationFunc /pulsar/examples/go-examples/exclamationFunc.go
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>/go/src/github.com/apache/pulsar/pulsar-function-go/pb/InstanceCommunication.pb.go:29:2: cannot find package "google.golang.org/grpc" in any of:
          /usr/local/go/src/google.golang.org/grpc (from $GOROOT)
          /go/src/google.golang.org/grpc (from $GOPATH)
  /go/src/github.com/apache/pulsar/pulsar-function-go/pb/InstanceCommunication.pb.go:30:2: cannot find package "google.golang.org/grpc/codes" in any of:
          /usr/local/go/src/google.golang.org/grpc/codes (from $GOROOT)
          /go/src/google.golang.org/grpc/codes (from $GOPATH)
  /go/src/github.com/apache/pulsar/pulsar-function-go/pb/InstanceCommunication.pb.go:31:2: cannot find package "google.golang.org/grpc/status" in any of:
          /usr/local/go/src/google.golang.org/grpc/status (from $GOROOT)
          /go/src/google.golang.org/grpc/status (from $GOPATH)
  12:13
  </code></pre>
  
  <p>The google.golang.org/grpc package is included in the <code>go.mod</code> file, which looks like this:</p>
  
  <pre><code>module github.com/apache/pulsar/pulsar-function-go
  go 1.13
  require (
      github.com/apache/pulsar-client-go v0.0.0-20200116214305-4d788d9935ed
      github.com/golang/protobuf v1.3.2
      github.com/sirupsen/logrus v1.4.1
      github.com/stretchr/testify v1.3.0
      golang.org/x/tools v0.0.0-20200119215504-eb0d8dd85bcc // indirect
      google.golang.org/grpc v1.26.0
      gopkg.in/yaml.v2 v2.2.2
  )
  </code></pre>
  
  <p>From the answer below, I was told that I can insert a <code>replace</code> directive in the Go module to allow us to point to local files when I build the Go binaries for the tests. 
  However, I don’t think I’m able to install the module correctly, perhaps due to the way the pulsar-function-go/examples are included in the module.
  So, I can't figure out what to set as the <code>/local/path/to/package</code> because I can't install the package successfully. (Maybe I'm confused about this too?)</p>
  
  <p>Is there a better way that we could structure the module/code to enable the installation of the local pulsar-function-go module into the test Docker image?</p>
  
  <p>If you want to look at the Go code yourself, see it here: <a href="https://github.com/apache/pulsar/tree/master/pulsar-function-go" rel="nofollow noreferrer">https://github.com/apache/pulsar/tree/master/pulsar-function-go</a>
  The Dockerfile is here: <a href="https://github.com/apache/pulsar/blob/master/tests/docker-images/latest-version-image/Dockerfile" rel="nofollow noreferrer">https://github.com/apache/pulsar/blob/master/tests/docker-images/latest-version-image/Dockerfile</a></p>