Cannot find nested package - go

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.

Related

Go import file from another directory

I'm trying to import utility into github_events.go.
utility.go is placed under services directory.
utility.go looks like this:
package utility
import (
"os"
"regexp"
)
My project structure looks like this:
This is how import from github_events.go
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
"sort"
"github-app/services/utility"
)
I also tried with an alias utility "github-app/services/utility"
But I get the following error could not import github-app/services/utility (no required module provides package "github-app/services/utility")compilerBrokenImport
My go.mod file:
module github-app
go 1.18
What am I doing wrong?
Just import "github-app/services" and you're good to go.

Import Github package in go

I'm trying out AWS Lambda in go and have the following imports.
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
)
Among these I'm unable to resolve the import for package aws-sdk-go. It always suggests to create such directory in GOPATH:/src.
Here's what I have tried so far:
Checked GOPATH is right.
I also did go mod vendor which resulted in vendor directory in my project and it has aws-sdk-go under it
Did go get github.com/aws/aws-sdk-go and this downloaded this sdk to my GOPATH and it looks like this pkg/mod/github.com/aws/aws-sdk-go#v1.44.138/aws
When I try to use pkg/mod/github.com/aws/aws-sdk-go#v1.44.138/aws in my import, it complains that there cannot be # in import.
I don't get why it doesn't refer to the package in vendor directory just like the other import github.com/aws/aws-lambda-go/lambda
Any ideas what's going on?
As indicated in the comments above, the issue was not with go code itself but the IDE.
I use IntelliJ and had GOPATH set and Module integration disabled.
To resolve the issue I removed GOPATH and enabled Module integration.
To understand Modules this blog helped me: https://go.dev/blog/using-go-modules

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 local package in plugin

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.

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