rand package error in Go - go

I try to call the ExpFloat64() function of the rand package (http://golang.org/pkg/rand/). However, it gives following error "prog.go:4: imported and not used: rand prog.go:7: undefined: ExpFloat64". Can anybody help me why is it giving error ? Code given below.
package main
import "fmt"
import "rand"
func main() {
fmt.Println(ExpFloat64())
}

The error message explains it perfectly - in Go, you can't import packages and not use them. Here, it says you're importing rand and not using it, so either use it or don't import it. Your main function should be:
fmt.Println(rand.ExpFloat64())

To add to what Chris Bunch said, if you really wanted to use the names in the package (e.g. ExpFloat64) directly without using the package name, you can do this:
import . "rand"

Related

How to import and use protobuf timestamppb package in a proto file?

I want to use timestamppb package in my protobufs because it helps to easily convert Timestamp to Go time. Time. However, I can't figure out how to import it into the .proto file. When I try I get the following error Import "google.golang.org/protobuf/types/known/timestamppb" was not found or had errors.
I looked at the documentation timestamppb docs for the timestamppb package but it seems there are no examples of how to use it in .proto files.
syntax = "proto3";
import "google.golang.org/protobuf/types/known/timestamppb";
// import "google.golang.org/protobuf/types/known/timestamppb.proto"; I tried this too but no luck
message Example {
timestamppb.Timestamp example_time = 1;
}
The import for .proto files is:
import "google/protobuf/timestamp.proto";
The one that you tried is the path that is needed in Go Code in combination with go get.

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

Go reports "undefined" for external package exported constant

Here's the code:
package main
import (
"log"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)
func main() {
log.Print(gopacket.MaxEndpointSize)
log.Print(pcap.MaxBpfInstructions)
}
When I run go build I get this:
./main.go:11: undefined: pcap.MaxBpfInstructions
But you can see MaxBpfInstructions right here: https://godoc.org/github.com/google/gopacket/pcap#pkg-constants
I feel this must be a stupid mistake, but I can't find it. Help?
It seems I was missing libpcap-dev. Now why Go or the package didn't throw a proper error message is beyond me.

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.

Import web.go error after using goinstall

With halfdans advice, I was successfully able to use goinstall github.com/hoisie/web.go without any errors after installing git first. However, now when I try to compile the sample code given, go is not finding the web package. I get the error,
main.go:4: can't find import: web
On this code
package main
import (
"web"
)
func hello(val string) string { return "hello " + val }
func main() {
web.Get("/(.*)", hello)
web.Run("0.0.0.0:9999")
}
Is there something special I need to do in order for it to recognize the package? I found the package source at $GOROOT/src/pkg/github.com/hoisie/web.go/web. I tried github.com/hoisie/web.go/web as the import and it still did not like that.
If you install web.go through goinstall, you need to do:
import "github.com/hoisie/web.go"
Goinstall is still an experimental system. It would be nice if you didn't have to include the full path.
import web "github.com/hoisie/web.go"

Resources