cannot find local package in plugin - go

I am writing a plugin in Golang and I am trying to import a package, one level up from the plugin. But, I cannot possibly find the package.
The package name is: com_styx_proto
path of plugin: SomeCoolUser/go/src/go_poc/plugins/styxBotDetectGrpc/styxBotDetectGrpc.go
path of package trying to access: SomeCoolUser/go/src/go_poc/plugins/styx.pb.go
This code is not working:
import (
"com_styx_proto"
"io/ioutil"
"net/http"
"time"
"fmt"
)
func main() {
fmt.Println("Hello World")
}
Error when building: cannot load go_poc/plugins: malformed module path "go_poc/plugins": missing dot in first path element

To import a package that is not part of the standard library, use its filesystem path. For your case, if you're trying to import the package under .../go_poc/plugins, write:
import (
com_styx_proto "go_poc/plugins"
)
Also, it is common practice to use the last component of the directory as the package name, so consider changing your directory structure to match your package names.

Related

Use Absolute path instead for go "fmt" in import directive

This is an instructional question and not a procedural one as simply requiring "fmt" works just fine, but when with the hello world golang file I modify it as follows
package main
import "golang.org/fmt"
func main() {
fmt.Println("Hello, world")
}
I get in response:
go:3:8: no required module provides package golang.org/fmt; to add it:
go get golang.org/fmt
I can see the fmt package in /usr/local/go/src/fmt and it mirrors the files in https://golang.org/src/fmt/
I am probably very close in the above file, what is the correct absolute path that would work to include fmt ? Thank you!
The correct absolute import path for the package is fmt.
Relative import paths start with ./ or ../. The import path fmt is an absolute import path.
Remote import paths start with a domain name. The package does not have a remote import path.
The tool chain creates a unique package for each import path. If the application could refer to the source code for the fmt package using a remote import path, the package with the remote path will be different from the standard fmt package. Every aspect of the package is unique. The code is duplicated. There is a ScanState type for each package and these types cannot be used interchangeably.
The pp cache is duplicated. And so on.
In this case, fmt is the fully qualified path. Compare the fmt docs [1] with golang.org/x/text docs [2].
Go standard library does have a go.mod [3], but trying to import std/fmt doesn't work either.
https://pkg.go.dev/fmt
https://pkg.go.dev/golang.org/x/text
https://github.com/golang/go/blob/master/src/go.mod

undefined main - testing code unable to acess main package

In the below code:
../folder1/some_test.go
package main_test
import "testing"
func TestF(t *testing.T) {
main.F()
}
../folder1/some_file.go
package main
func F() {
}
main.F() gives undefined main
$ go version
go version go1.14.3 linux/amd64
Renaming package name from main_test to main resolve the problem.
Why main_test package name is not allowed for testing code, in same folder?
You must import the package to use the package.
go.mod:
module example.app
main.go:
package main
func F() {}
func main() {}
main_test.go
package main_test
import (
"testing"
"example.app" // import the main package
)
func TestF(t *testing.T) {
main.F()
}
The code above assumes that main*.go are in the same directory as go.mod for package example.app. Replace example.app with the name of your module. Adjust the paths to match your application. For example, if the the main*.go files are in the directory cmd/example below the directory containing go.mod, use the import path example.app/cmd/example instead of example.app.
Note that a test can import a main package this way, but non-test code cannot.
The problem is that main_test is a different package from main.
To access functions in main you need to import main and access the functions like this: main.F()
Also, note f starts with lowercase and thus is not exported from the package main. To access it in main_test it needs to be exported (which can be done by changing it to start with a capital letter: F).
Alternatively, you can change the test file to be in package main.
Edit with a note: When importing main note that import paths are by directory name. Usually developers in Go put their packages in directories with the same name as the package (e.g. put main in a directory named main). In your case the package and directory names are different so the import will be import ".../folder1" not import ".../main". You'll still be able to use main.F() to access the function:
package main_test
import (
"testing"
"../folder1" // use the full path to folder1 from the root of your module
)
func TestF(t *testing.T) {
main.F()
}

Can't import a package from a different file in GO

I am using go1.13.4 and below is my project structure:
src/types/type.go
src/utils/util.go
go.mod
In go.mod:
module example.com/graphql
go 1.13
require github.com/graph-gophers/graphql-go v0.0.0-20191031232829-adde0d0f76a3
In src/types/type.go:
package types
type USER struct {
...
}
In src/utils/util.go,
package utils
import (
"example.com/graphql/types"
"fmt"
"io/ioutil"
"os"
)
I got tan error when build the project:
$ go build ./...
src/utils/utils.go:4:2: cannot find module providing package example.com/graphql/types
I wonder why it can't find the package example.com/graphql/types? I am reading https://blog.golang.org/using-go-modules and I already set the module name in go.mod file at the root of my project.
With your current layout, import path of types is example.com/graphql/src/types.
go.mod should be inside src if you have that structure. Or better would be to get rid of src. go.mod must be next to the types and utils folders.

Cannot find nested package

I have a project with the following structure:
myapp/
-services/
-services/
-exch.go
-services.go
-server.go
Having $GOPATH set to /home/dev/dev/go
this is how server.go names it's package and imports:
//server.go
package main
import (
"net/http"
"github.com/labstack/echo"
"myapp/services"
)
this is services.go:
//services.go
package services
import (
"fmt"
"myapp/services/exch"
)
and this is exch.go:
//exch.go
package exch
import (
"net/http"
"fmt"
"io/ioutil"
"encoding/json
)
Now, server.go imports package services fine, but services.go cannot find package exch. I tried changing the imports path several ways but cannot make it work. Am I missing something?
Might be useful to know that /myapp is located here: /home/dev/dev/go/src
One directory per package, one package per directory. If exch.go is supposed to be imported as services/exch, it needs to be in a directory services/exch, not in directory services/services.

My main file can't find another file I created that I imported into the main file

I have the following file structure:
- project/
- src/
- main/
- main.go
- viewmodels/
- home.go
- public/
My project is found in:
~/go/src/
When I attempt to run my main file it throws the error:
src/main/main.go:10:2: cannot find package "viewmodels" in any of:
/usr/local/Cellar/go/1.5.3/libexec/src/viewmodels (from $GOROOT)
/Users/nicholasrucci/go/src/viewmodels (from $GOPATH)
It looks like main is looking for package viewmodels in the wrong location. From my understanding, after reading How to Write Go Code and the previous programs would run fine, my configuration is set up correctly, but obviously something is wrong.
Go related configurations from .zshrc:
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/opt/go/libexec/bin
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:/usr/local/go/bin
Snippet of main.go:
package main
import (
"bufio"
"log"
"net/http"
"os"
"strings"
"text/template"
"viewmodels"
)
Snippet of home.go:
package viewmodels
import ()
Any direction for what is going on and how I can fix this issue would be great. Thanks.
You should use the full package name for the import path: "project/src/viewmodels" in this case, assuming project is under /Users/nicholasrucci/go/src, but I would structure your project folder differently (no src folder for example)
Alternatively you could set your GOPATH to the fully qualified path to your project folder, which would then allow your main.go to import "viewmodels" as you have it.
This works just fine for me:
src/main/main.go
package main
import (
"viewmodels"
)
func main() {
viewmodels.Something()
}
src/viewmodels/home.go
package viewmodels
import ()
func Something() {
}
I'm guessing it is your env variables.

Resources