How to use statistical function (MannWhitneyUTest) from go internal packages - go

I am trying to run Mann-Whiteney-U test with following code:
package main
import (
"fmt"
"stats"
)
func main() {
e, _ = MannWhitneyUTest([]float64{1, 2, 3, 4, 5},
[]float64{1, 2, 3, 5, 6},
0)
fmt.Println("Mann-WhitneyUTest: ", e)
}
However, this gives me this error:
$ go run mainstats2.go
mainstats2.go:5:2: cannot find package "stats" in any of:
/usr/local/go/src/stats (from $GOROOT)
/home/iuser/go/src/stats (from $GOPATH)
I have following stats packages installed:
$ go list all | grep stats
github.com/montanaflynn/stats
github.com/montanaflynn/stats/examples
golang.org/x/perf/internal/stats
golang.org/x/perf/vendor/github.com/aclements/go-moremath/stats
golang.org/x/perf/vendor/google.golang.org/grpc/stats
I need stats package golang.org/x/perf/ which I had installed by command: go get golang.org/x/perf/internal/stats
I believe this package is already there in go installation and was not needed to be installed separately.
How do I solve this problem? Thanks for your help.

The error occurs because your import path is incorrect, it should be:
import (
"fmt"
"golang.org/x/perf/internal/stats"
)
But even though import path issue is fixed, you'll get another error for trying to use internal package.
stats.go:4:5: use of internal package golang.org/x/perf/internal/stats not allowed
I suggest try to find another alternative library.
EDIT #1:
If you insist, there is a workaround. Try to copy the $GOPATH/src/golang.org/x/perf/internal/stats folder directly into your project, then import it. It worked, please see screenshot below.
EDIT 2:
I have copied the folder to ~/go/src/stats. It is still not working. What should I put for import. Currently it is just "stats"
I think you are doing it wrong. First, you need to create what-so-called project, it's a folder placed inside $GOPATH/src.
For example in image below I created a project called my-example-app, placed under $GOPATH/src. So the full path of the project will be $GOPATH/src/my-example-app.
Inside my project, I created main.go file. This file contains the code (I copied from yours).
Also, I copied the $GOPATH/src/golang.org/x/perf/internal/stats folder into my project, so the stats folder will be on the same level with my main.go.
The import of stats folder need to be happen relative to the project name, so the correct import path would be:
import "my-example-app/stats"
Here is content of my main.go (copied from yours with some syntax error fix addition).
package main
import (
"fmt"
"my-example-app/stats"
)
func main() {
e, _ := stats.MannWhitneyUTest([]float64{1, 2, 3, 4, 5},
[]float64{1, 2, 3, 5, 6},
0)
fmt.Println("Mann-WhitneyUTest: ", e)
}

Related

Can't import packages

I'm trying to import a subdirectory I have in my project to my main file, but for some reason I get this error:
could not import ./service (no package for import ./service)
This is how I import:
import (
"os"
"service"
)
I also tried "service/app"
"./service/app"
"./service" nothing works, always the same error.
I'm trying to use the app.go file
This is the file structure:
Project Directory
-- main.go
-- service (directory)
-- app.go (file)
I tried to restart VS Code, tried to use go install/update tools, nothing works.
Also, this is my main func:
func main() {
a := &service.App()
a.Initialize(
os.Getenv("APP_DB_USERNAME"),
os.Getenv("APP_DB_PASSWORD"),
os.Getenv("APP_DB_NAME"),
)
a.Run(":8010")
}
&service.App() does not show a problem, but when I remove the "service" import, it does say
undeclared name: service
So I don't understand what the problem is.
This error sometime show on the "os" import too, I don't know why.
Golang doesn't support relative imports.
In modules, there finally is a name for the subdirectory. If the parent directory says "module m" then the subdirectory is imported as "m/subdir", no longer "./subdir".
So, in your case you should import service package as your_module_name/service.

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.

Leekchan Accounting conflicting with ShopSpring Decimal

I'm trying out the samples at https://github.com/leekchan/accounting
package main
import (
"fmt"
"math/big"
"github.com/shopspring/decimal"
"github.com/leekchan/accounting"
)
func main() {
ac := accounting.Accounting{Symbol: "$", Precision: 2}
fmt.Println(ac.FormatMoney(123456789.213123))
}
and using them exactly as showing on Github I'm getting the following error:
Failed parsing input: package "github.com/shopspring/decimal" is imported from multiple locations: "/users/dev/go/src/github.com/shopspring/decimal" and "/users/dev/go/src/github.com/leekchan/accounting/vendor/github.com/shopspring/decimal"
I can't leave out:
github.com/shopspring/decimal
or I'll get another error. I tried:
"dec" github.com/shopspring/decimal
but that doesn't change anything. Shouldn't it?
Weird thing is I can't find any details about this anywhere. Am I really the only person getting this, or am I missing something completely obvious?
This is because they have a checked in vendor directory that contains github.com/shopspring/decimal which it appears you already have on your path. You are best having a single vendor directory inside your project that contains both github.com/shopspring/decimal and github.com/leekchan/accounting so that you directory structure will look like:
- main.go
| - vendor
| - github.com/leekchan/accounting
| - github.com/shopspring/decimal

Cannot find package "api/handlers"

Ok so this question has been asked before and I believe I have looked through all of them to and tested the answers but I will explain how each one doesn't match my case. I might have missed the answer in one of those but I read each one and attempted to see if it could fit my case
How to import local packages in go? I believe my imports follow the structure of the answer
Go build: "Cannot find package" (even though GOPATH is set) I'm not sure if this one is fully pertinent but I don't think it's the same error.
Golang - Why can't I import local package in GOPATH/src/project but can in home directory? My import path isn't relative so this question isn't pertinent.
My error is simple:
cannot find package "api/handlers" in any of:
C:\Go\src\api\handlers (from $GOROOT)
C:\Projects\Go\src\api\handlers (from $GOPATH)`
My project structure is as follows:
src
|
--api
|
-- index.go
-- repo.go
|
github.com
|
main.go
Environment variables:
$GOPATH : C:\Projects\Go
$GOROOT : C:\Go\
index and repo.go both have the same package name, imports, and just an empty function:
package handlers
import (
"net/http"
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
}
My main.go:
package main
import (
"fmt"
"log"
"net/http"
"api/handlers"
)
func main() {
http.HandleFunc("/api/index", handlers.indexHandler)
http.HandleFunc("/api/repo", handlers.repoHandler)
log.Fatal(http.ListenAndServer("localhost:8080", nil))
}
What's happening is that import api/handlers is looking for the folder handlers in the folder api, and then looking in the contents for the package name. Add a handlers folder inside api and move index.go and repo.go into that folder. Or just change the package name to api in those files and just do import api.
About your comment:
cannot refer to unexported name handlers.indexHandler
In order for you to be able to use function indexHandler from your main package, you should rename it to IndexHandler. In go, for things to be able to be accessed by other packages, the name needs to start with a capital letter.

How to use self defined package

I have follow two simple go files
main.go
package main
import "fmt"
import "go-package/math"
func main() {
xs := []float64{1, 2, 3, 4}
avg := math.Average(xs)
fmt.Println(avg)
}
mymath.go
package math
func Average(xs []float64) float64 {
total := float64(0)
for _, x := range xs {
total += x
}
return total / float64(len(xs))
}
The file and directory structure is:
$GOPATH
src
go-package
math
mymath.go
main.go
When I navigate to $GOPATH/src/go-package, and open the terminal and run the command: go run main.go, the result is printed out.
Then, I navigate to $GOPATH/src/go-package/math, and run the command go install, and the package successfully installed at $GOPATH\pkg\windows_amd64\go-package\math.a
Then I navigate back to $GOPATH/src/go-package, and remove the math directoy,
I rerun the command go run main.go, error occurs:
main.go:4:8: cannot find package "go-package/math" in any of:
D:\softwareInstalled\Go\src\go-package\math (from $GOROOT)
D:\softwareInstalled\Go\GoPath\src\go-package\math (from $GOPATH)
I have put the math package at $GOPATH\pkg\windows_amd64\go-package\math.a
I wonder why Go doesn't pick up the package from this directory.
And how can I use math.a?Where should I place it?
Golang uses the source files of your imported packages. So when you wanna use the package go-package/math inside your main.go, you should not delete the source files from src/go-package/math. Leave them inside your project and everything's good.
Besides, go get also loads source files into your $GOPATH/src folder. Hope this helps.

Resources