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.
Related
I am still learning Go, and I am stuck at this issue where I cannot use the struct I created in the main package
app.go
package main
import (
"fmt"
"log"
"database/sql"
// struct named App to hold our application
type App struct {
Router *mux.Router
DB *sql.DB
}
main_test.go
package main_test
import (
"testing"
"log"
"os"
)
// application we want to test
var a main.App // 👈 throws error
When I run the test go test -v, it throws the following error:
# pg_mux_test [pg_mux.test]
.\main_test.go:10:7: undefined: main 👈
FAIL pg_mux [build failed]
Disclaimer: I may not be giving a precise answer since it's not clear about your project structure, still making an attempt since I was at first very confused with imports in Go. And this is answer is valid only if you are using go packages (with go mod init stuff).
As an example, say your go.mod package name is main and your folder structure is something like:
.
├── app.go
├── go.mod
└── someDirName // (say main_test)
└── main_test.go
Then, you can import main package in your main_test.go as follows:
package main_test
import (
// ... other imports
"main" // 👈 import it here
)
// application we want to test
var a main.App // 👈 this time, it wont' throw error
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()
}
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.
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.
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.