I am aware of Golang requirements.txt equivalent , but the context of the question is rather different.
I am trying to optimize builds of golang programs inside a docker container. My dockerfile looks something like this:
FROM golang:1.12.5 as builder
WORKDIR $GOPATH/src/test-ldap/
COPY main.go .
RUN go get -d -v ./...
...
while my main.go
looks like
package main
import (
"log"
"fmt"
"gopkg.in/ldap.v3"
)
func main() {
...
Of course, every time I make a change in the source code, the docker layer
COPY main.go .
is changed, hence the go get
command needs to be re-run and cannot be reused from the docker build cache even though the import
block is unchanged.
Now of course I could type something like
RUN go get -d -v log fmt gopkg.in/ldap.v3
and place this before the COPY
statement, but this violates the so-called single source of truth principle. I would then have to change the same thing in two different places in my codebase if I will ever wish to add extra imports.
How can I store my import requirements in a separate file? What is an idiomatic way to do this in go development?