Using Go version go1.17.1 linux/amd64 on my CentOS 7. GOPATH and GOROOT are set in my .bashrc in the order:
export GOPATH="${HOME}/.go"
export GOROOT="/usr/local/go"
export PATH="$PATH:${GOPATH}/bin:${GOROOT}/bin"
Got my project in /home/wsb/_projects/local/parlance:
[wsb#localhost local]$ pwd
/home/wsb/_projects/local
[wsb#localhost local]$ tree
.
└── parlance/
└── main.go
└── utils.go
From the terminal, using go get stores src in /home/wsb/.go/pkg/mod/cache... folder
[wsb#localhost parlance]$ go get -v -u github.com/gorilla/mux
go: downloading github.com/gorilla/mux v1.8.0
github.com/gorilla/mux
[wsb#localhost parlance]$ go build main.go
main.go:3:8: no required module provides package github.com/gin-gonic/gin: go.mod file not found in current directory or any parent directory; see 'go help modules'
[wsb#localhost parlance]$
[root#localhost /]# find -iname gin
./home/wsb/.go/pkg/mod/cache/download/github.com/gin-gonic/gin
Why would it throw error on build?
Using go mod is the key here for managing packages for any particular projects.
go mod is used for installing and tracking external packages outside of packages in GOPATH.
From within existing project root directory parlance we initialize go mod using:
[wsb#localhost parlance]$ go mod parlance
It creates two files within the project root directory: go.mod and go.sum.
Now we can go get any module we will use specifically for our project.
go get github.com/gin-gonic/gin
go get github.com/gofiber/fiber/v2
This creates external package requirement details in go.mod and their respective checksum in go.sum file.
Finally, we can build/run our package like
go build main.go
go run main.go
Related
i have a Revel project and i can't start it because everytime i run go mod tidy, it looks like finding module but it's not putting the found module in go.sum
here's my go env
GOENV = C:\Users\mycomp\AppData\Roaming\go\env
GOMOD = C:\Program Files\Go\src\myproject\go.mod
GOPATH = C:\Users\micha\go
go version
go version go1.18.3 windows/amd64
revel version
Revel executing: displays the Revel Framework and Go version
Revel Framework : Unknown (1.1.0 remote master branch)
Revel Cmd : 1.1.2 (1.1.2 remote master branch)
Revel Modules : Unknown (1.1.0 remote master branch)
go mod tidy
go: finding module for package github.com/PaesslerAG/jsonpath
go: finding module for package github.com/tdewolff/test
go: finding module for package github.com/PaesslerAG/gval
go: found github.com/tdewolff/test in github.com/tdewolff/test v1.0.7
go: found github.com/PaesslerAG/gval in github.com/PaesslerAG/gval v1.2.0
go: found github.com/PaesslerAG/jsonpath in github.com/PaesslerAG/jsonpath v0.1.1
go mod init myproject seem working because it create go.mod but it's just odd because it only creates this line, usually it creates a lot of modules i need
module myproject
go 1.18
then when i type go mod tidy, it creates empty go.sum. What do i miss ?
Cause the problem is too complex to reproduce online, i started to debug and reinstall my Go from scractch, between solving a bug where my program can't run i tried several things that might be useful
after Go 1.15 or 1.17 , my Go project can't run properly, so i put it in C:\Program Files\Go\src\<project_name>
The problem that i have is in GOPATH (usually in C:\Users\<comp_name>\go) don't have src folder, that makes it found downloaded module but can't seem to put it in go.mod inside my project
So in the end i reinstall all my Go and Revel. Here's how to delete all your associated Go to really really delete all your files
Delete Go Folder in C:\Program Files\Go\
Delete go folder in C:\Users\<comp_name>\go
View all hidden files and delete folder go-build in C:\Users\<comp_name>\AppData\Local
View all hidden files and delete folder go if any in C:\Users\micha\AppData\Roaming
I choose the way Go Module Specifics to build my Go app.
When I set the directory like below, it works well.
main.go
go.mod
go.sum
README.md
where go.mod
module xxx
// +heroku goVersion go1.18
go 1.18
...
But I want to put the all source code in the src folder. and push to Heroku, it will wrong
README.md
📂 src
- main.go
- go.mod
- go.sum
Is there a way to do this?
Your go.mod (and go.sum) files need to be in the codebase root directory. Buildpack will search the codebase for main package(s) and compile them all. You can override this behavior (what get's build) by using +heroku install build directive:
When using go modules, this buildpack will search the code base for main packages, ignoring any in vendor/, and will automatically compile those packages. If this isn't what you want you can specify specific package spec(s) via the go.mod file's // +heroku install directive (see below).
README.md
- go.mod
- go.sum
📂 src
- main.go
I don't think you can use Go Module Specifics with go.mod file on deeper level than root in Heroku but your code can indeed be in subdirectory.
Im trying to rebuild a golang github repository to apply some minor changes.
The go application Im trying to modify is the following https://github.com/lian/msfs2020-go
Please use the provided github link to inspect the file tree.
I used the master branch and extracted it to /user/Documents/msfs2020-go-master
If I call go build from /user/Documents/msfs2020-go-master the output equals: no Go files in /user/Documents/msfs2020-go-master
I tried deleting the go.mod and recreating it with go mod init github.com/lian/msfs2020-go followed with a go mod tidy
but still no Go files in /user/Documents/msfs2020-go-master
Here the current go.mod
module github.com/lian/msfs2020-go
go 1.16
require github.com/gorilla/websocket v1.4.2
And here the go.sum
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
The master was build with 1.14 and Im using 1.16 golang.
All my test go applications/modules run/build/install fine at a "Hello World" developing level.
What did I do wrong? I gladly accept any input and will provide additional information's if requested.
The command go build builds the package in the current working directory. The command reports an error because there is not a package at the root of the repository.
Fix by building the package containing the command. Any of the following will work from the root of the repository:
go build ./vfrmap
or
cd vrfrmap
go build
or
go build github.com/lian/msfs2020-go/vfrmap
See also the file build-vfrmap.sh.
I'm currently using Dep and would like to start using Go modules.
How do I migrate?
Migrating from Dep to Go Modules is very easy.
Run go version and make sure you're using Go version 1.11 or later.
Move your code outside of GOPATH or set export GO111MODULE=on.
go mod init [module path]: This will import dependencies from Gopkg.lock.
go mod tidy: This will remove unnecessary imports, and add indirect ones.
(Optional) Delete your vendor folder (rm -rf vendor/ or move to trash)
go build: Do a test build to see if it works.
rm -f Gopkg.lock Gopkg.toml: Delete the obsolete files used for Dep.
Go has imported my dependencies from Dep by reading the Gopkg.lock file and also created a go.mod file.
If you want to keep your vendor folder:
Run go mod vendor to copy your dependencies into the vendor folder.
Run go build -mod=vendor to ensure go build uses your vendor folder.
To add to #Nicholas answer's:
Here is from the offical golang documenation:
To create a go.mod for an existing project:
Navigate to the root of the module's source tree outside of GOPATH:
$ export GO111MODULE=on # manually active module mode
$ cd $GOPATH/src/<project path> # e.g., cd $GOPATH/src/you/hello
Create the initial module definition and write it to the go.mod file:
$ go mod init
This step converts from any existing dep Gopkg.lock file or from any of the other nine total supported dependency formats, adding require statements to match the existing configuration.
Build the module. When executed from the root directory of a module, the ./... pattern matches all the packages within the current module. go build will automatically add missing or unconverted dependencies as needed to satisfy imports for this particular build invocation:
$ go build ./...
Test the module as configured to ensure that it works with the selected versions:
$ go test ./...
(Optional) Run the tests for your module plus the tests for all direct and indirect dependencies to check for incompatibilities:
$ go test all
Another way to upgrade to modules.
Remove the Gopkg.toml and Gopkg.lock
rm Gopkg.*
Initialise the Go modules
GO111MODULE=on go mod init
Run go mod tidy to pull all the indirect modules and remove unused modules
GO111MODULE=on go mod tidy
Run build to ensure everything works fine
go build
Tip in case you face few modules not found then manually update the modules tag in go.mod file.
I have go.mod file inside root/src/abc. And in root/build-scripts I have a script which does go get. As I am using Go 1.11 I have not used the go path instead the mod file in root/src/abc takes care of other imports except for the packages that are being used in build script which gives error:
go: cannot determine module path for source directory.
Any suggestions?
It's hard to say anything with certainty without seeing the actual commands you run, by it seems your scripts do not change the working directory, and therefore the go commands they execute are not in the module's root folder or any of its subfolders.
Quoting from Command Go: The go.mod file:
A module version is defined by a tree of source files, with a go.mod file in its root. When the go command is run, it looks in the current directory and then successive parent directories to find the go.mod marking the root of the main (current) module.
So your scripts should change the working directory to root/src/abc or any of its subfolders, else the go command will not find the go.mod file.
According to Go documents, go get is deprecated since Go 1.17. However, running command go install may give similar errors. Here is my case. When I ran go install in a directory where there' s a go.mod file, the error was gone. You may try to create a new go.mod and check whether or not the issue is there.
% go install
go: cannot find main module, but found .git/config in /Users/xxxx/Documents/learn-terraform-lambda-api-gateway
to create a module there, run:
cd .. && go mod init
% go mod init
go: cannot determine module path for source directory /Users/xxxx/Documents/learn-terraform-lambda-api-gateway/hello-world
(outside GOPATH, module path must be specified)
Example usage:
'go mod init example.com/m' to initialize a v0 or v1 module
'go mod init example.com/m/v2' to initialize a v2 module
Run 'go help mod init' for more information.
% go mod init example.com/m
go: creating new go.mod: module example.com/m
go: to add module requirements and sums:
go mod tidy
% go mod tidy
go: finding module for package github.com/aws/aws-lambda-go/lambda
go: found github.com/aws/aws-lambda-go/lambda in github.com/aws/aws- lambda-go v1.32.0
go: downloading github.com/stretchr/testify v1.6.1
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
% go install