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.
Related
I have following folder structure here
server
-main.go
-cmd
-config
-config.go
-handlerr.go
-handlers
-handlers.go
-pkg
-models
-models.go
-db
-db.go
-router
-router.go
and when I am trying to import the "models package" into db package it says "invalid import path:...",this structure i am following with book ,so what am i doing wrong ?How do i import the models into db functions or should i replace them (db and models),any suggest?
enter image description here
Here are the files in the question filled out. The import from db to models is illustrated. I set the module name to "my.example". Change that name to meet your needs. It's best to pick something including a "." to avoid conflict with a standard package. You can run this code on the Go PlayGround. How to Write Go Code explains all of this stuff in detail.
-- main.go --
package main
import (
"my.example/pkg/models/db"
)
func main() {
db.Hello()
}
-- go.mod --
module my.example
-- cmd/config/config.go --
package config
-- cmd/config/handlerr.go --
package config
-- handlers/handlers.go --
package handlers
-- pkg/models/models.go --
package models
func Hello() string { return "hello from models" }
-- pkg/models/db/db.go --
package db
import (
"fmt"
"my.example/pkg/models"
)
func Hello() {
fmt.Println("hello from db and", models.Hello())
}
-- pkg/router/router.go --
package router
Regarding the layout of the files: This is in the land of opinions, so I will just ask you a question. Does the extra level of nesting in the pkg and cmd gain you anything?
UPDATE:
As #maxm reminded me, use GOPATH, here is a great example, that he mentioned.
So, if my web search and a little example program are correct, this is pretty much what is happening here.
Golang sees local packages as folders with .go files, on practice /modules/module.go if modules.go has package modules on the 1st line.
So, when we are including a local package, we are actually including a folder. I created a little example:
/project
main.go
/modules
modules.go
/printer
printer.go
main.go
package main
import (
"./modules/printer"
)
func main() {
printer.Print()
}
modules/modules.go
package modules
import (
"fmt"
)
func Modules() {
fmt.Println("Modules Package")
}
modules/printer/printer.go
package printer
import (
"../../modules"
)
func Print() {
modules.Modules()
}
And this actually works! So, you can import a parent module, you just need to include the folder.
Of course, we cannot have import cycle, Golang won't allow it and if you are having this kind of error, it's better to change the structure of your program.
I'm not talking about paths and stuff, it's just a practical solution. So, smart people, please correct me!
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.
I want to step through my program using Godebug. However because I'm using net/http I get errors such as:
/home/heath/go/src/net/http/h2_bundle.go:45:2: could not import golang_org/x/net/http2/hpack (cannot find package "golang_org/x/net/http2/hpack" in any of:
/home/heath/go/src/golang_org/x/net/http2/hpack (from $GOROOT)
/x/net/http2/hpack does exist in my GOPATH but in ~heath/go/src/golang.org ... not golang_org (not sure what's happening there)
I have read that this error occurs because godebug doesn't support http2 yet (can't find source).
I have tried to disable http2server and http2client by setting the GODEBUG env both in init() and on the command line. I have also confirmed that these settings are being set by doing a fmt.Println("GODEBUG", os.Getenv("GODEBUG"). As per instructions located here
GODEBUG=http2client=0 # disable HTTP/2 client support
GODEBUG=http2server=0 # disable HTTP/2 server support
My simple code example to replicate the error is:
package main
import "fmt"
import "net/http"
import "os"
func init() {
os.Setenv("GODEBUG", "http2server=0,http2client=0")
}
func main() {
fmt.Println("GODEBUG", os.Getenv("GODEBUG"))
_ = "breakpoint"
fmt.Println("Hello, World!")
http.ListenAndServe(":8080", nil)
}
godebug run example.go
I am running Go version:
go version go1.7 linux/amd64
This is probably not the correct way to do things. But I copied the http2 package from my vendor directory under $GOROOT/src/vendor to the src directory under my $GOPATH/src.
If anyone has any further input on the correct way to reference vendor directories please add your thoughts. Just putting my workaround here in case someone else comes across the same issue.
Edit: Actually a 'nicer' way to do things was to do an ln -s ..src/vender/github_org to ../src/github_org
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"
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"