Internal packages in Go - 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.

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

How to import files from current directory in go

Intro
I'm trying to import my EventController.go to my main.go file.
Directory:
├───Controllers
│ └───Event
│ └───EventController.go
├───Models
├───Routes
│
└ Main.go
Problem:
import (
"log"
"net/http"
_ "/Controllers/Event/EventController.go" //problem here
)
error : cannot import absolute path
I read some documentation but the thing is I see that I'm doing it correctly, though i learnt about $GOPATH but I want to use the local directory thing.
What am I doing wrong and what's this error about
NOTE: I want add that I'm using windows as os
Thank you.
There are a few issues:
Packages are imported, not files (as noted in other answers)
File absolute import paths are not valid as the error states. An application can use file relative import paths (the path starts with "./") or a path relative to the Go workspace. See the go command doc for info on the syntax. Import paths relative to the Go workspace are the preferred form.
It is idiomatic to use lowercase names for packages (and their corresponding directories). The camel case names in the question are workable, but it's better to go with the flow.
The document How to Write Go Code is a nice tutorial on how to do this.
Here's how to reorganize the code given the above. This assumes that main.go is in a package with import path "myapp". Change this import path to whatever you want.
-- main.go --
package main
import (
"log"
_ "myapp/controllers/event"
)
func main() {
log.Println("hello from main")
}
-- go.mod --
module myapp
-- controllers/event/eventController.go --
package event
import "log"
func init() {
log.Println("hello from controllers/event")
}
Run this example on the Go playground.
You cannot import a file. You can import a package. So, lets say your main is the package "github.com/mypackage", then you should import "github.com/mypackage/Controllers/Event".
Go supports package level import. You can import a package by adding it to the import statement at the beginning of the file.
In your case, you should do something like this-
import (
"log"
"net/http"
"Controllers/Event/EventController"
)
Also, you should remove first "/" from the file name
_ /Controllers/Event/EventController.go" //problem here
because your Controllers folder is at the same level as Main.go file. You should always give relative path in the import statements.
In this way, you can use any file which is listed under EventController folder.

How to import local package in 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()

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