undefined main - testing code unable to acess main package - go

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()
}

Related

Golang Module problem--package xxx/xxxx is not in GOROOT

so here is what my directory is:
go
|-src
|-ppppppSample
|-newFolderOne
|-firstSample.go
|-hello.go
|-go.mod
and here is the content of hello.go
package main
import (
"fmt"
jjj "ppppppSample/newFolderOne"
)
func main() {
fmt.Println("start to test")
fmt.Println(jjj.FirstVVVV)
}
here is the content of firstSample.go
package newFolderOne
var FirstVVVV = "Im first SSSSSSSSSSSS"
and here is my go.mod's content
module mmmmmppppp
go 1.15
when giving it the cmd go run hello.go, the terminal came out with like this:
D:\Users\eien_zheng\go\src\ppppppSample>go run hello.go
hello.go:5:2: package ppppppSample/newFolderOne is not in GOROOT (C:\Go\src\ppppppSample\newFolderOne)
So here is my question:
(since I'm new for golang, I wish you guys could understand and tolerate some of my misunderstanding)
According to my understanding to Go module (maybe it's wrong), the Go module's function is gonna let some kind of online resource be downloaded to the directory GOPATH/pkg/mod instead of existing in GOROOT.
No matter which directory your project in, your project can still import those resource from GOPATH/pkg/mod if you init Go module.
But!!, in my understanding, it still can use package system to import package around project directory, in the meantime import online resource by Go module system.
How is that when I do (mod init) for hello.go, then it loses the (basic package import function) for this project?
|--src
|--sample
|--newFolder
|-firstSample.go (package xyz)
|--hello.go (package main import(xyz "sample/newFolder")
|--go mod (module sample go 1.15)
go mod should reference the root folder , here the root folder is |--sample
module sample
go v1.xx
inside hello.go;
package main
import ( xyz "sample/newFolder")
and make sure exported functins or variables use camelCase aka starts with BlockLetters.
Import packages within a module using the module's path:
package main
import (
"fmt"
jjj "mmmmmppppp/newFolderOne"
)
...
Run it on the Playground.

Can't find custom package

I'm tryng to create a custom package in go: i created folder project:
my_project
|_database
|_database.go
main.go
but when i try to import it gaves me this error: "could not import database (no package for import database)"
i try to run "go init" as written in some tutorial and created a "go.mod" file, then i run "go install" and works fine, but in the main.go it still not working.
The project structure is now this:
my_project
|_database
|_database.go
|_go.mod
|_go.sum
main.go
I work on windows with visual studio code
Here is how i import the package:
import (
"fmt"
...
"database"
)
function main() {
You need to do a few things.
Make sure your database.go is the following way:
it should contain a package name this case is database, comment the
function/s that need to be used outside this package, use func instead
of function. and comment code and capitalize function name so it can
be exported.
package database
import "fmt"
// Connect function to connect to my database
func Connect(){
fmt.Print("connected...")
}
your main.go should be the following.
use func instead of function, import database based on folder position
this case is ./ use the package name database. because this function
does not belong from current package main.
package main
import "./database"
func main() {
database.Connect()
}
This should be your folder structure.
my_project
|_database
|_database.go
main.go
go.mod
go.sum

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.

How to write packages in go?

go is my first statically typed and compiled language so I am not sure about how to go about doing some things like writing packages...Should I write a package like...
package mypkg
import "fmt"
func mypkg {
fmt.Println("mypackage")
}
and then import this into the main package....install it and then test if it works?
Or should I write mypkg like this...
package main
import "fmt"
func mypkg() {
fmt.Println("mypackage")
}
func main() {
mypkg()
}
Should I do it this way to make sure that I can call go run mypkg and see if it works, then delete the main function and rename the package and then work on the rest of the library?
Or did I miss the point entirely and I should do something else?
Here are some basic steps to get you going if you want to use packages.
Create a folder called mypkg in the folder you're working in. In that folder create a mypkg.go file with the following code (note the capital letter in the function name. You need to do this so main.go can see it).
package mypkg
import "fmt"
func Mypkg {
fmt.Println("mypackage")
}
Now install the package just by typing go install while you're still in your mypkg directory. This will create a linkable object file you can use.
Now go back to your starting directory and create main.go with the following code:
package main
import (
"...(path to your mypkg folder)/mypkg"
"fmt"
)
func main() {
mypkg.Mypkg()
//optional
//fmt.Println("whatever")
}
Now run it by typing go run main.go. This will compile and run main.go that uses the previous mypkg package you created.

Go subpackage functions not imported properly

Trying to wrap my head around packages in Golang.
This is my workspace
/bin
/pkg
/src
/github.com
/esbenp
/testrepo
/subpackage
somefuncs.go
main.go
main.go
package main
import "github.com/esbenp/testrepo/subpackage"
func main() {
Somefunc()
}
somefuncs.go
package subpackage
import "fmt"
func Somefunc() {
fmt.Printf("yo")
}
I was under the impression that since Somefunc starts with an uppercase letter it would be exported for use in other files that imported it. The ouput I get in the console is.
main.go:4: imported and not used: "github.com/esbenp/testrepo/subpackage"
main.go:8: undefined: Somefunc
Can someone point me in the right direction?
You have to prefix the function by the name of the package is belongs to: subpackage.Somefunc().
In the case you have several subpackages with the same name, you have to alias them while importing them, or there will be a conflict:
import (
xapi "x/xx/xxx/api"
yapi "y/yy/yyy/api"
)
When you import a package it will be made available under its name.
To address Somefunc in your main.go you have to do
subpackage.Somefunc()

Resources