Could not import local modules in Golang - go

I am trying to import local modules, but I am unable to import it using go mod. I initially built my project using go mod int github.com/AP/Ch2-GOMS
Note my environment is go1.14 and I am using VSCode as my editor.
This is my folder structure
Ch2-GOMS
│ ├── go.mod
│ ├── handlers
│ │ └── hello.go
│ └── main.go
My main.go code:
package main
import (
"log"
"net/http"
"os"
"github.com/AP/Ch2-GOMS/handlers" // This gives "could not import github.com/AP/Ch2-GOMS/handlers" lint error
)
func main() {
l := log.New(os.Stdout, "product-api", log.LstdFlags)
hh := handlers.NewHello(l)
sm := http.NewServeMux()
sm.Handle("/", hh)
http.ListenAndServe(":9090", nil)
}
I cannot see auto-complete for my local modules such as handlers.NewHello.
go build generated go.mod contents:
module github.com/AP/Ch2-GOMS
go 1.14
I am also getting You are neither in a module nor in your GOPATH. Please see https://github.com/golang/go/wiki/Modules for information on how to set up your Go project. warning in VScode, even though i have set GO111MODULE=on in my ~/.bashrc file

Read: Ian Lance Taylor's comment (Go's Core Team)
I know of three ways:
Method 1 (The best way):
# Inside
# Ch2-GOMS
# │ ├── go.mod
# │ ├── handlers
# │ │ └── hello.go
# │ └── main.go
# In Ch2-GOMS
go mod init github.com/AP/Ch2-GOMS
# In main.go
# Add import "github.com/AP/Ch2-GOMS/handlers"
# But, make sure:
# handlers/hello.go has a package name "package handlers"
You must be doing something wrong and that's why it's not working.
Method 2 (The good way):
# Inside
# Ch2-GOMS
# │ ├── go.mod
# │ ├── handlers
# │ │ └── hello.go
# │ └── main.go
# Inside the handlers package
cd Ch2-GOMS/handlers
go mod init github.com/AP/Ch2-GOMS/handlers # Generates go.mod
go build # Updates go.mod and go.sum
# Change directory to top-level (Ch2-GOMS)
cd ..
go mod init github.com/AP/Ch2-GOMS # Skip if already done
go build # Must fail for github.com/AP/Ch2-GOMS/handlers
vi go.mod
Inside Ch2-GOMS/go.mod and add the following line:
# Open go.mod for editing and add the below line at the bottom (Not inside require)
replace github.com/AP/Ch2-GOMS/handlers => ./handlers
# replace asks to replace the mentioned package with the path that you mentioned
# so it won't further look packages elsewhere and would look inside that's handlers package located there itself
Method 3 (The very quick hack way for the impatient):
Turn off Go Modules GO111MODULE=off
Remove go.mod file
# Check: echo $GOPATH
# If $GOPATH is set
mkdir -p $GOPATH/src/github.com/AP/Ch2-GOMS
cd $GOPATH/src/github.com/AP/Ch2-GOMS
# If $GOPATH is unset
mkdir -p ~/go/src/github.com/AP/Ch2-GOMS
cd ~/go/src/github.com/AP/Ch2-GOMS
# Now create a symbolic link
ln -s <full path to your package> handlers
Reason: During the build, the compiler first looks in vendor, then GOPATH, then GOROOT. So, due to the symlink, VSCode's go related tools will also work correctly due to the symlink provided as it relies on GOPATH (They don't work outside of GOPATH)

Below are the steps-
on main folder - go mod init
2.go mod tidy
3.go to the folder where main file is present
4.install the package via
go get <package name>
5.go build
Before above steps your project path should be
project path = GOPATH/src/<project_name>
Along with there should be 2 more folder parallel with src folder
src
pkg
bin
when ever you install any package it should be go inside pkg folder
and after doing go mod tidy there should be one file generated
go.mod
List item

go mod tidy alone at root folder did it for me

If you want to import local modules, you need to map the module path such that it can find the code in your local file system.
First use the go mod edit command to replace any imports to module to the local file
$ go mod edit -replace example.com/greetings=../greetings
The command specifies that example.com/greetings should be replaced with ../greetings for the purpose of locating the dependency. After you run the command, the go.mod file in the current directory should include a replace directive in its mod file
After that use the go mod tidy command to synchronize dependencies, adding those required by code where you imported but not yet traced by the current module
$ go mod tidy
Referred from the official documentation

Related

SIMPLE godoc Hello world

Trying to serve a godoc on a simple, flat code folder. The online docs do not explain how to achieve this SIMPLE task.
So, creating this simple structure,
/tmp/testgodoc$ tree
.
└── src
├── main (just the binary)
└── main.go
1 directory, 2 files
where main.go is simply
/tmp/testgodoc$ cat src/main.go
// Hello godoc
package main
import "fmt"
// Say Hello
func main() {
fmt.Println("Hello")
}
When running either in GOPATH or module modes, opening localhost:6060 in a browser does not give the expected result of documenting current folder.
Running in module mode give this output and result:
/tmp/testgodoc$ ~/go/bin/godoc -goroot=. -http=:6060
using module mode; GOMOD=/dev/null
(when Ctrl-C:) cannot find package "." in:
/src/main
^C
And running in GOPATH mode seems to point to the local standard library:
/tmp/testgodoc$ GO111MODULE=off ~/go/bin/godoc -goroot=. -http=:6060
using GOPATH mode
^C
You should put your main package into a subdirectory, maybe like this:
~/go/src/testGoDoc$ tree
├── cmd
│ └── main.go
├── go.mod
└── pkg
└── test1
└── test_package.go
and by this you can run both commands:
godoc -http=:6060 #http://localhost:6060/pkg/<module name inside go.mod>/
and
GO111MODULE=off godoc -http=:6060 #http://localhost:6060/pkg/testGoDoc/

How to run sibling Go applications (modules) from the parent directory

I have multiple Go projects (and all of them are also Go modules) all in a folder. They are all HTTP servers and exchanging REST calls, thus, I need all of them up and running simultaneously.
So, for local testing purposes, I thought it would be reasonable to run all of them from the parent instead of moving all of the project root directories and running go run main.go in multiple terminals.
container_dir/
├── prj1/
│ ├── go.mod
│ ├── main.go
│ └── ...
├── prj2/
│ ├── go.mod
│ ├── main.go
│ └── ...
└── ...
Here are some some commands I have tried and the error messages for each time:
container_dir $ go run ./*/*.go
##ERROR: named files must all be in one directory; have ./prj1/ and ./prj2/
container_dir $ go run *.go
##ERROR: stat *.go: no such file or directory
container_dir $ go run ./prj1 ./prj2/
##ERROR: cannot find package "github.com/jackc/pgx/v4" in any of:
/usr/local/go/src/github.com/jackc/pgx/v4 (from $GOROOT)
/home/user/go/src/github.com/jackc/pgx/v4 (from $GOPATH)
cannot find package ...
So, I can give a final rephase for the question: How to run multiple go modules in sibling directories when they have some third party dependencies etc.?
P.S: As possible with Go modules suggest container_dir for my projects is in an arbitrary location and I expect no $GOPATH relevance.
Go version: 1.13.6
Don't use go run outside of tiny, playground-style tests
Source paths are a compile-time concern and are irrelevant at runtime - being in "sibling directories" doesn't mean anything when the program is running
go run runs a single program; just like building a program and running its executable binary (which is what go run does) runs a single program.
Looks like your go.mod stuff is having problems dude.
remember you can do replace inside it and reference your other application
module container_dir/prj2
go 1.13
require(
container_dir/prj1 v0.0.0
)
replace (
container_dir/prj1 => ../prj1
)
require is the path you import but it'll get switched to the relative path on build.

How to pass arguments via go generate

Here is how my project is structure
├── cmd
│   ├── orders
│   │   ├── main.go
└── scripts
└── codegenerator.go
the codegenerator.go file is the file where i have put my code to generate the code. Here is the logic for codegenerator.go
rename main.go to old_main.go
read from old_main.go line by line and write to a new file called main.go
insert new lines/code blocks based on markers/placeholder in main.go
remove old_main.go file
The go:generate directive is in main.go like this
//go:generate go run ../../scripts/codegenerator.go
I ran go generate from command line and wanted to pass in 3 arguments so that they can be utilized in codegenerator.go. here is my command and the errors i got (i executed this command on path cmd/orders)
$ go generate arg_one arg_two arg_three
can't load package: package arg_one: unknown import path "arg_one": cannot find module providing package arg_one
can't load package: package arg_two: unknown import path "arg_two": cannot find module providing package arg_two
can't load package: package arg_three: unknown import path "arg_three": cannot find module providing package arg_three
So the questions are
Am i running go generate properly?
How can i pass in arguments from go generate command line to the target script
While I agree you may be better off using another solution, there isn't anything preventing usage of env vars (os specifics may vary)
➜ /tmp cat foo.go
package main
//go:generate python run.py $ARG
func main() {}
➜ /tmp cat run.py
import sys
print sys.argv[1]
➜ /tmp ARG=poop go generate foo.go
poop

How to fix Go build error "can't load package" with Go modules?

I'm setting up a new project using Go modules with this tutorial, and then trying to build it.
The module is located in a folder outside of the $GOPATH with the following structure:
example.com
├── my-project
├── ├── main
├── ├── ├── main.go
├── ├── go.mod
I've run go mod init example.com/my-project in directory example.com/my-project and created the go.mod file shown above.
main.go has basic contents:
package main
import (
"fmt"
)
func main(){
fmt.Println("Hello, world!")
}
After attempting to run go build in directory example.com/my-project, I receive the following error message:
can't load package: package example.com/my-project: unknown import path "example.com/my-project": cannot find module providing package example.com/my-project.
I've also attempted to run go build in directory /, outside of example.com/my-project, and I get similar, failing results:
can't load package: package .: no Go files in ...
I'm probably getting some basic thing wrong, so thanks for your patience and any help you can give.
no need for the directory main,
just move your main.go and go.mod to example.com/my-project and it will work.
Project root should look like:
.
├── go.mod
└── main.go
In my case it was that the variables GOMOD and GOWORK were taking other values different from the project I solved it by executing the command go env and verifying the values of those variables and deleting the files of that address.
Then I removed the go.mod and go.sum file from the project and ran the following commands again:
go mod init projectName
go mod tidy
go run ./...
And it worked perfectly.

How to use "internal" packages?

I try understand how to organize go code using "internal" packages. Let me show what the structure I have:
project/
internal/
foo/
foo.go # package foo
bar/
bar.go # package bar
main.go
# here is the code from main.go
package main
import (
"project/internal/foo"
"project/internal/bar"
)
project/ is outside from GOPATH tree. Whatever path I try to import from main.go nothing works, the only case working fine is import "./internal/foo|bar". I think I do something wrong or get "internal" package idea wrong in general. Could anybody make things clearer, please?
UPDATE
The example above is correct the only what I need was to place project/ folder under $GOPATH/src. So the thing is import path like the project/internal/foo|bar is workable if we only import it from project/ subtree and not from the outside.
With modules introduction in Go v1.11 and above you don't have to specify your project path in $GOPATH/src
You need to tell Go about where each module located by creating go.mod file. Please refer to go help mod documentation.
Here is an example of how to do it:
project
| go.mod
| main.go
|
\---internal
+---bar
| bar.go
| go.mod
|
\---foo
foo.go
go.mod
project/internal/bar/go.mod
module bar
go 1.14
project/internal/bar/bar.go
package bar
import "fmt"
//Bar prints "Hello from Bar"
func Bar() {
fmt.Println("Hello from Bar")
}
project/internal/foo/go.mod
module foo
go 1.14
project/internal/foo/foo.go
package foo
import "fmt"
//Foo prints "Hello from Foo"
func Foo() {
fmt.Println("Hello from Foo")
}
project/main.go
package main
import (
"internal/bar"
"internal/foo"
)
func main() {
bar.Bar()
foo.Foo()
}
Now the most important module
project/go.mod
module project
go 1.14
require internal/bar v1.0.0
replace internal/bar => ./internal/bar
require internal/foo v1.0.0
replace internal/foo => ./internal/foo
Couple things here:
You can have any name in require. You can have project/internal/bar if you wish. What Go think it is URL address for the package, so it will try to pull it from web and give you error
go: internal/bar#v1.0.0: malformed module path "internal/bar": missing dot in first path element
That is the reason why you need to have replace where you tell Go where to find it, and that is the key!
replace internal/bar => ./internal/bar
The version doesn't matter in this case. You can have v0.0.0 and it will work.
Now, when you execute your code you will have
Hello from Bar
Hello from Foo
Here is GitHub link for this code example
The packages have to be located in your $GOPATH in order to be imported. The example you gave with import "./internal/foo|bar" works because it does a local-import. internal only makes it so code that doesn't share a common root directory to your internal directory can't import the packages within internal.
If you put all this in your gopath then tried to import from a different location like OuterFolder/project2/main.go where OuterFolder contains both project and project2 then import "../../project/internal/foo" would fail. It would also fail as import "foo" or any other way your tried due to not satisfying this condition;
An import of a path containing the element “internal” is disallowed if
the importing code is outside the tree rooted at the parent of the
“internal” directory.
Now if you had the path $GOPATH/src/project then you could do import "foo" and import "bar" from within $GOPATH/src/project/main.go and the import would succeed. Things that are not contained underneath project however would not be able to import foo or bar.
below way is more scalable, especially when you plan to build multiple binaries
github.com/servi-io/api
├── cmd/
│ ├── servi/
│ │ ├── cmdupdate/
│ │ ├── cmdquery/
│ │ └── main.go
│ └── servid/
│ ├── routes/
│ │ └── handlers/
│ ├── tests/
│ └── main.go
├── internal/
│ ├── attachments/
│ ├── locations/
│ ├── orders/
│ │ ├── customers/
│ │ ├── items/
│ │ ├── tags/
│ │ └── orders.go
│ ├── registrations/
│ └── platform/
│ ├── crypto/
│ ├── mongo/
│ └── json/
The folders inside cmd/ represents the number of binaries you want to build.
for more
Also to check: When you are using your externally imported object types: make sure you are prefixing them with the namespace they're in. As a golang newbie, I didn't know I had to do that, and was wondering why is VS Code just removing my import (for not being used) when I saved. It's because I had to prefix the imported object with the namespace name:
Example:
import (
"myInternalPackageName" // works fine as long as you follow all instructions in this thread
)
//Usage in code:
myInternalPackageName.TheStructName // prefix it, or it won't work.
If you don't put the namespace prefix before the object/struct name, VS code just removes your import for being unused, and then you still have the error: "Can't find TheStructName"... That was very confusing, and I had to do a build without VS code through the command line to understand that.
The point is: I had to specify the package prefix when actually using the struct from that internal package.
If you don't want to use the qualifier prefix when using imported objects, use:
import . "thePath" // Can use contents without prefixing.
Reference: What does the '.' (dot or period) in a Go import statement do?

Resources