Error message go: go.mod file not found in current directory or any parent directory; see 'go help modules'
Yes, just follow the tutorial and for me that was doing go mod init test3
to create a module. No one else has been upgrading from the old version or everyone else just understood it properly I guess.
Error Message : go: go.mod file not found in current directory or any parent directory;
You need to add a go.mod file to your project's root directory.
Use modules to manage dependencies. Official docs: https://go.dev/blog/using-go-modules
Example:
go mod init project-name
go mod init example.com/project-name
go mod init github.com/you-user-name/project-name
You may need to use the tidy command to clean up after running one of the above commands.
go mod tidy
Use the path format from above when importing a package into your go file
Example:
import (
// Import internal and external packages like this
"github.com/you-user-name/project-name/package-name"
// Import standard library packages the normal way
"testing"
"math/rand"
)
go: go.mod file not found in current directory but it already exist in the directory
Okay I solved my problem.
First, my WORKDIR wasn't pointing at the right directory : WORKDIR /go/src/github.com/rosmo/gcs2bq
instead of WORKDIR /work/src/github.com/rosmo/gcs2bq
but it's only because of me using /work instead of /go for the installed packages.
Then I added the follwing after the COPY main.go .
command :
RUN go mod init v1
RUN go mod tidy
Which created the "missing" go.mod file and properly installed the dependencies/packages needed.
The rest of the build went perfectly normal.
Thanks for your help.
Docker build : go: go.mod file not found in current directory or any parent directory
The credit goes to @TheFool, thank you for the documents and guidance.
I read official Docker documents : docs.docker.com/language/golang/ and also multi staging build blog
Here is the solution I came up with and I can boot my Go application container locally
Dockerfile of mine:
FROM golang:1.17 AS build
WORKDIR /
COPY . .
RUN go mod init feedme
RUN go mod tidy
RUN go install github.com/go-delve/delve/cmd/dlv@latest
RUN go build -gcflags="all=-N -l" -o /feedme
RUN echo $(ls /go/bin)
FROM gcr.io/distroless/base-debian10
WORKDIR /
EXPOSE 2345
COPY --from=build /go/bin/dlv /dlv
COPY --from=build /feedme ~/feedme
#ENTRYPOINT [ "/dlv" ]
CMD ["/dlv", "--listen=:2345", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "~/feedme"]
I boot my container using :
docker run -p 2345:2345 <docker image ID>
Then I tried to curl to it , it does have response:
curl http://localhost:2345
[Edit] Per suggestion from TheFool, I used my local copy of go.mod and go.sum directly in my container. COPY it from my local workspace to container,(rather than generate go.mod in the container) to avoid any unexpected surprise in the future:
Here is the improved version of Dockerfile
FROM golang:1.17 AS build
WORKDIR /
COPY go/app/parsedata-xml-fp.go .
COPY go.mod . # just copy local go.mod
COPY go.sum .
RUN go install github.com/go-delve/delve/cmd/dlv@latest
RUN go build -gcflags="all=-N -l" -o /feedme
RUN echo $(ls /go/bin)
FROM gcr.io/distroless/base-debian10
WORKDIR /
EXPOSE 2345
COPY --from=build /go/bin/dlv /dlv
COPY --from=build /feedme ~/feedme
#ENTRYPOINT [ "/dlv" ]
CMD ["/dlv", "--listen=:2345", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "~/feedme"]
no required module provides package imported-package : go.mod file not found in current directory or any parent directory;
This link posted in a now deleted comment - https://go.dev/ref/mod#mod-commands - provides an explanation for this seemingly (GO)PATH breaking change:-
Most go commands may run in Module-aware mode or GOPATH mode. In
module-aware mode, the go command uses go.mod files to find versioned
dependencies, and it typically loads packages out of the module cache,
downloading modules if they are missing. In GOPATH mode, the go
command ignores modules; it looks in vendor directories and in GOPATH
to find dependencies.As of Go 1.16, module-aware mode is enabled by default, regardless of
whether a go.mod file is present. In lower versions, module-aware mode
was enabled when a go.mod file was present in the current directory or
any parent directory.
Further:-
Module-aware mode may be controlled with the GO111MODULE environment
variable, which can be set to on, off, or auto.If GO111MODULE=off, the go command ignores go.mod files and runs in GOPATH mode.
By turning GO111MODULE off I am able to use GOPATH as documented.
Errors running the first Go project on the local machine
You should not need a GOPATH
environment variable with Go 1.16.
Only:
GO111MODULE=on
(won't be needed in Go 1.17 or 1.18)GOPROXY=https://proxy.golang.org,direct
GOROOT=C:\path\to\go
(GOROOT
unless you have installed Go in its default folder: %USERPROFILE%\go
)
I tried:
D:\git> git clone https://github.com/rrrkren/topshot-sales
Cloning into 'topshot-sales'...
remote: Enumerating objects: 25, done.
remote: Counting objects: 100% (25/25), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 25 (delta 9), reused 21 (delta 6), pack-reused 0
Receiving objects: 100% (25/25), 16.95 KiB | 5.65 MiB/s, done.
Resolving deltas: 100% (9/9), done.
D:\git> cd topshot-sales
D:\git\topshot-sales> go run main.go
go: downloading github.com/onflow/flow-go-sdk v0.10.0
...
go: downloading gopkg.in/yaml.v2 v2.2.5
panic: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp 35.193.214.129:9000: i/o timeout"
goroutine 1 [running]:
main.handleErr(...)
D:/git/topshot-sales/main.go:14
main.main()
D:/git/topshot-sales/main.go:23 +0x805
exit status 2
No error about go.mod
, only a runtime execution error.
no required module provides package fyne.io/fyne/app: go.mod file not found in current directory or any parent directory on intel mac
Since Go 1.16 modules are required so you need to run go mod init <project name>
.
Note also that for Fyne you should use the v2 imports now, which are ”fyne.io/fyne/v2”
. And you should install the fyne
tool from fyne.io/fyne/v2/cmd/fyne
.
Related Topics
Deleting String Up to the First Occurrence of Certain Character
Why Does Perf Show That Sleep Takes All Cores
How to Configure a Systemd Service to Restart Periodically
Delete Files with String Found in File - Linux Cli
Show Special Characters in Unix While Using 'Less' Command
Replace Only If String Exists in Current Line
Finding Executable Files Using Ls and Grep
Use Bash to Read a File and Then Execute a Command from the Words Extracted
Opening a .Tar.Gz File with a Single Command
How to Determine Ssl Cert Expiration Date from a Pem Encoded Certificate
Why Does Order in Which Input Libraries Are Specified Matter
Embedding the Password in the Bash Script
How to Limit the Number of Results Returned from Grep
How to Do Division with Variables in a Linux Shell
How to Install SQL * Plus Client in Linux