How to import local package in Go? - go

how can i do to import a package?
myproject
&nbsp&nbspmain.go
&nbsp&nbspRepository
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp DBHelper.go
&nbsp&nbspConfiguration
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Configuration.go
I need to call a function inside DBHelper.go from Configuration.go, how can i do it?
I need to call Select function from Configuration.go
Thanks! Regards!
I Call Select but not working(undefined: DBHelper.Select in DBHelper)

Package imports are relative from your GOPATH
For example, if DBHelper.go uses package "helpers", your import would look like this:
<name of project folder>/Repository/helpers
Here is a very similar question on the subject:
How to use custom packages in Go
--Edit
Almost forgot, to perform the actual function call, you must use the package name / alias.
helpers.MyFunc()

Related

Cannot import package "google.golang.org/api/compute/v1"

I am using cloud function to auto create some VM instance. Before, I was success in my use-case. You can view it here.Now, I want to update some line in metadata script. But when I save the code, it tell me some package I was import cannot find. Logs is
Build failed: src/cloudfunctions/function.go:9:2: cannot find package "google.golang.org/api/compute/v1" in any of:
/usr/local/go/src/google.golang.org/api/compute/v1 (from $GOROOT)
/workspace/src/google.golang.org/api/compute/v1 (from $GOPATH); Error ID: 2f5e35a0
But I was view document in https://pkg.go.dev/google.golang.org/api/compute/v1, usage example import like this import "google.golang.org/api/compute/v1", as same as me.
So, who can tell me what is $GOROOT and $GOPATH in cloud function, and how can I import this package.
Thanks in advance.
Run
go get -u -x google.golang.org/api/compute/v1
Then cd/open your directory in terminal then run
go mod init filename.go
then
go mod tidy
Also restart your IDE as that will need to update

Undefined function in Go located at local package

I'm new with Go and I'm trying to build an API, but I'm having some problems at importing functions located at another local package.
My folder structure looks like the following:
├── app.go //at package "main"
└── middleware
├── authentication.go // package "middleware"
I'm trying to import the functions inside the authentication.go file like that:
package main
import (
"fmt"
"log"
"net/http"
"./middleware" //Also tried "middleware"
)
Thinking that's an import problem because if I move the functions to the same package and folder, it works fine.
The func I'm trying to use also starts with caps, so there shouldn't be a problem importing it:
func AuthMiddleware(next http.Handler) http.Handler {
//...
}
What's wrong with my code? Also, what's the best way to import a local package without having to write the whole path?
I'm using Go 1.15.2, and checked all related S.O posts, but none looks to solve my problem.
EDIT :
Whenever you use a function located at another package, you have to refer first to the package. I was trying to call my function as AuthMiddleware(parameters) but the right way to call it was middleware.AuthMiddleware(parameters).
Don't use dot import paths like this, it is not recommended. Use the relative path for your go src dir (which can be local only, it doesn't have to be online) and it'll work fine.
So something like
import "me.com/api/middleware"
Then to avoid stutter perhaps:
middleware.Auth()
There is two way to call function from another package
You can use package or alias to call function
With package name
package main
import (
"./middleware"
)
middleware.Auth();
With alias
package main
import (
mid "./middleware"
)
mid.Auth();
You can import whole package by using . , after that you do not need to use alias or package name to call function. You can use call function directly with function name
package main
import (
. "./middleware"
)
Auth();

Internal packages in Go

How to import internals packages in Go ?
import (
"runtime/internal/atomic"
"runtime/internal/sys"
)
Like this without get a error:
imports runtime/internal/atomic: use of internal package not allowed
And use internal funcs in a main package ?
Background
Go encourages structuring a program as a collection of packages interacting using exported APIs. However, all packages can be imported. This creates a tension when implementing a library or command: it may grow large enough to structure as multiple packages, but splitting it would export the API used in those additional packages to the world. Being able to create packages with restricted visibility would eliminate this tension.
A rule proposed to Go 1.4
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.
Short answer
You can't (at least easily) and you shouldn't.
I will show you how I use internal nettest package:
// I copied nettest to vendor with `dep ensure` I think. Then:
mkdir vendor-local
cp -r vendor/golang.org/x/net/internal/nettest ./vendor-local/nettest
vim ./vendor-local/nettest/stack.go and remove import comment // import "foo" [1]
// Use full import in your go file:
import "github.com/foo-user/bar-project/vendor-local/nettest"
[1]: https://github.com/golang/net/blob/a8b9294777976932365dabb6640cf1468d95c70f/internal/nettest/stack.go#L6
Docs about import comments
You may find all import comments in your internal package with grep -r "// import" ./vendor-local/nettest
Why can't I copy nettest to ./vendor and use shorter import
You can, but utils like dep ensure that don't support local packages will purge your copy.

Q: Getting Build Error "Invalid Import Path"

I'm stuck on running the BeeGO app using "bee run" it says
The thing is I've already have setup properly my GOPATH to D:/Web Dev/GO/BeeGO/test-project/
and also routers path does exist and I've tried to manual build the file but it doesn't generate an .exe file.
Anyone knows how to fix this?
I'm using Windows 8.1 Pro (64-bit)
Thanks
GO expects the directory structure under $GOPATH in following ways as described in code organization:
$GOPATH/src <--- where your source code goes
/pkg
/bin
Instead of placing your source files directly under $GOPATH (D:/Web Dev/GO/BeeGO/test-project/ for your case), you want to move your code under $GOPATH/src.
D:/Web Dev/GO/BeeGO/test-project/src/main.go
D:/Web Dev/GO/BeeGO/test-project/src/quickstart/routers/routers.go
D:/Web Dev/GO/BeeGO/test-project/src/quickstart/controllers/controllers.go
import path should be always starting from $GOPATH/src. routers.go can be always imported as import "quickstart/routers" and controllers.go can be imported as import "quickstart/controllers".
That's not how you import a package.
The import path is relative to $GOPATH/src. use:
import "quickstart/routers"
Finally fixed the bug from the framework,
What I did:
in main.go import from
"D:/Web Dev/GO/BeeGO/test-project/quickstart/routers"
I changed it to _"../quickstart/routers" make sure to include _ this means to import the library even if it is not used,
Then in the routers/router.go I changed the import path
"D:/Web Dev/GO/BeeGO/test-project/quickstart/controllers" to "../controllers"
It seems BeeGO doesn't generate the template properly and caused the build to fail.
Another possiblity for this error, is when you copy-paste code from the internet,
and
import "quickstart/routers"
became
import "quickstart/routers "
due to bugs in some CMS/Blog systems (notice the space at the end before the closing quote...).

Import and not used error

I'm getting below error with below import code:
Code:
package main
import (
"log"
"net/http"
"os"
"github.com/emicklei/go-restful"
"github.com/emicklei/go-restful/swagger"
"./api"
)
Error:
.\main.go:9: imported and not used: "_/c_/Users/aaaa/IdeaProjects/app/src/api"
Is there a reason why the import is not working given that I have package api and files stored under api folder?
I'm using below to use api in main.go
func main() {
// to see what happens in the package, uncomment the following
restful.TraceLogger(log.New(os.Stdout, "[restful] ", log.LstdFlags|log.Lshortfile))
wsContainer := restful.NewContainer()
api := ApiResource{map[string]OxiResp{}}
api.registerLogin(wsContainer)
api.registerAccount(wsContainer)
api.registerLostLogin(wsContainer)
api.registerWallet(wsContainer)
}
The compiler looks for actual use of a package .. not the fact it exists.
You need to use something from that package.. or remove the import. E.g:
v := api.Something ...
If you don't use anything from that package in your source file .. you don't need to import it. That is, unless you want the init function to run. In which case, you can use the ignore notation import _.
EDIT:
After your update, it appears you're overwriting the package import here:
api := ApiResource{map[string]OxiResp{}}
That declares a variable called api. Now the compiler thinks its a variable, and so you're not actually using the api package.. you're using the api variable.
You have a few options.
Firstly, you can call that variable something else (probably what I would do):
apiv := ApiResource{map[string]OxiResp{}}
Or, alias your import (not what I would do.. but an option nonetheless):
import (
// others here
api_package "./api"
)
The problem is that the compiler is confused on what to use. The api package.. or the api variable you have declared.
You should also import the package via the GOPATH instead of relatively.
First of all, don't use relative imports.
If your GOPATH contains /Users/aaaa/IdeaProjects/app/src, then import your package as api.
Next, you're shadowing api with the assignment to api :=. Use a different name.
This error can occur if your .go file has an error that is preventing the compiler from reaching the location where it is used.

Resources