go mod does not include the non-go code in the vendor directory.
Currently we are using go with go-oracle to connect to the database. We are planning on using docker so went with the idea of using go modules to version-ize our project. But since go-oracle has sub folders which have C code, it does not get copied over from the mod directory that go creates in the pkg folder. Is there a way we could add the non go code as well? We did try to use https://github.com/goware/modvendor but it did not copy the non-go code. Unless we did not use it correctly.
package main
import (
"fmt"
"github.com/jmoiron/sqlx"
log "github.com/sirupsen/logrus"
goracle "gopkg.in/goracle.v2"
)
const connectionString = "some connection string"
func main() {
fmt.Print("Inside main")
db, err := sqlx.Connect("goracle", connectionString)
if err != nil {
log.Infof("Could not connect %v%", err)
} else {
db.Query("select 1 from dual")
}
fmt.Println(goracle.DriverName)
}
go mod init
go mod vendor
You will see that the code will not compile.
I believe your issue is that you did not set a module path for your package. Details on my actions to build your code below:
After creating a test package gomod-test in my /tmp folder, I ran:
go mod init tmp/gomod-test // <-- this is the module path
go mod vendor
which pulled in the required dependencies including gopkg.in/goracle.v2 inside of ./vendor/goracle.v2/. I was then able to build the "project" using
go build main.go
Related
I'm using the following Go module to build an application:
github.com/martinlindhe/unit
Right now, the Go code is very simple; I'm just trying to get the environment set up for the real work:
import(
"fmt"
"unit"
)
foo := unit.FromFahrenheit(100)
fmt.Println("100 fahrenheit in celsius = ", foo.Celsius())
In go.mod:
go 1.17
require github.com/martinlindhe/unit v0.0.0-20210313160520-19b60e03648d
Executing either go build or go get results in:
package unit is not in GOROOT (/usr/local/Cellar/go/1.17/libexec/src/unit)
Running go mod download executes without error. The go.sum file seems to be correct, all the necessary dependencies exist.
The environment is the latest version of VS Code, Go installed via homebrew on MacOS Big Sur 11.5.2
There must be something obvious I'm missing. I've not had this problem with other apps I've written.
The import path is not right. Playground.
package main
import (
"fmt"
"github.com/martinlindhe/unit"
)
func main() {
foo := unit.FromFahrenheit(100)
fmt.Println("100 fahrenheit in celsius = ", foo.Celsius())
}
In Go, I was trying simple database connection. I need to import gorilla/mux, but I couldn't.
I am using VS Code. After cd ing to my project directory, I created main.go and did run
go get -u github.com/gorilla/mux
Here is main.go
package main
import (
"database/sql"
"fmt"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 3000
user = "postgres"
password = "postgres"
dbname = "test1"
)
func main() {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfully connected!")
}
[Note that, after executing go get -u github.com/gorilla/mux terminal shows
C:\Go\src\github.com\IamFrost\go-rest-api>go get -v -u github.com/gorilla/mux
go: golang.org/x/text upgrade => v0.3.2
go: golang.org/x/crypto upgrade => v0.0.0-20200429183012-4b2356b1ed79
go: golang.org/x/sys upgrade => v0.0.0-20200430082407-1f5687305801
go: golang.org/x/net upgrade => v0.0.0-20200425230154-ff2c4b7c35a0
go: downloading golang.org/x/sys v0.0.0-20200430082407-1f5687305801
golang.org/x/sys/cpu
golang.org/x/crypto/chacha20poly1305
crypto/tls
]
Look i have no other syntax error in mini map. At red mark, when I put mouse, the hover text, its funny to me:
1)imported but not used
but the next line
2)no package for import github.com/gorilla/mux)
lol didn't that go against 1) ?
Someone please explain why that occurs
However,
After using go build in terminal
Here is terminal:
C:\Go\src\github.com\IamFrost\go-rest-api>go build
go: inconsistent vendoring in C:\Go\src:
github.com/gorilla/mux#v1.7.4: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
github.com/lib/pq#v1.4.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
golang.org/x/crypto#v0.0.0-20200429183012-4b2356b1ed79: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
golang.org/x/net#v0.0.0-20200425230154-ff2c4b7c35a0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
golang.org/x/sys#v0.0.0-20200430082407-1f5687305801: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
golang.org/x/crypto#v0.0.0-20200128174031-69ecbb4d6d5d: is marked as explicit in vendor/modules.txt, but not explicitly required in go.mod
golang.org/x/net#v0.0.0-20191126235420-ef20fe5d7933: is marked as explicit in vendor/modules.txt, but not explicitly required in go.mod
golang.org/x/sys#v0.0.0-20200201011859-915c9c3d4ccf: is marked as explicit in vendor/modules.txt, but not explicitly required in go.mod
run 'go mod vendor' to sync, or use -mod=mod or -mod=readonly to ignore the vendor directory
[Note : I did 'go mod vendor' also, but no change]
So someone point me why I can't import gorilla/mux or pq.
What else I have to do?
(and please explain what does that mean? is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt)
Here is how I fixed it for me:
go get ./...
go mod vendor
If your Go version is less than 1.13 then, you should prepend GO111MODULE=on in above commands.
I don't know why it works (I am beginner). I found something in net, somehow it worked .
Here is how :
This is my go directory C:\Go\ . But I create the project in my desktop folder
Then I recreated the project under C:\Go\src
(this is exact folder
C:\Go\src\github.com\[github_username]\[project_name] )
Then I pasted all the code at main.go
Finally from terminal go mod vendor
Then go build
Then go run main.go
Finally it worked
[I would like to know why it worked. I would really appreciate your comment : about why they are forcing me to go under C:\Go\src. I already have GOPATH C:\Go\ , GOBIN C:\Go\bin set in environment variable]
In the recent releases of Golang (1.11 onwards I think) there's been a change on the GOPATH variable. My understanding is you only need to set this if GOPATH will not be $HOME/go.
Go looks for your projects in $HOME/go/src/, for instance my Golang projects are in $Home/go/src/github.com/<your_github_userid>` directory, etc.
If you want to use a different location of your projects, you can still set the GOPATH to a different directory.
And my shell file has the following to accommodate both a set and not set GOPATH.
export PATH="$PATH:$(go env GOPATH)/bin"
You can get more information at the GOPATH environment documentation
go mod init ,
go mod tidy ,
go run :3
I working in default directory (C:\Go\src[projectName]) and run commands in project directory. If is not help u , move project to your GOPATH
New to Go/Golang and am trying to understand its package/dependency management system a little better.
I cloned this simple web service repo off GitHub and tried running it with go run main.go. In that main.go file:
package main
import (
"log"
"net/http"
"strconv"
"github.com/wpferg/services/httpHandlers"
"github.com/wpferg/services/storage"
"github.com/wpferg/services/structs"
)
const PORT = 8080
var messageId = 0
func createMessage(message string, sender string) structs.Message {
messageId++
return structs.Message{
ID: messageId,
Sender: sender,
Message: message,
}
}
func main() {
log.Println("Creating dummy messages")
storage.Add(createMessage("Testing", "1234"))
storage.Add(createMessage("Testing Again", "5678"))
storage.Add(createMessage("Testing A Third Time", "9012"))
log.Println("Attempting to start HTTP Server.")
http.HandleFunc("/", httpHandlers.HandleRequest)
var err = http.ListenAndServe(":"+strconv.Itoa(PORT), nil)
if err != nil {
log.Panicln("Server failed starting. Error: %s", err)
}
}
When I run this (run go main.go) I get:
main.go:8:2: cannot find package "github.com/wpferg/services/httpHandlers" in any of:
/usr/local/go/src/github.com/wpferg/services/httpHandlers (from $GOROOT)
/Users/myuser/go/src/github.com/wpferg/services/httpHandlers (from $GOPATH)
main.go:9:2: cannot find package "github.com/wpferg/services/storage" in any of:
/usr/local/go/src/github.com/wpferg/services/storage (from $GOROOT)
/Users/myuser/go/src/github.com/wpferg/services/storage (from $GOPATH)
main.go:10:2: cannot find package "github.com/wpferg/services/structs" in any of:
/usr/local/go/src/github.com/wpferg/services/structs (from $GOROOT)
/Users/myuser/go/src/github.com/wpferg/services/structs (from $GOPATH)
So it seems Go supports a way of "fetching" other packages from GitHub via HTTP but for some reason when I run it locally, its expecting the packages to be local.
What can I do to fix this so that the other packages are resolved? Why is Go looking for them locally instead of fetching them via the URL?
Problem is that this repo is from pre go modules era and doesn't use any dependency management system. Easiest way to fix it is to try to initialize it as module(if you use go < 1.14 set environment variable GO111MODULE=on):
go mod init github.com/wpferg/services
And then run:
go run main.go
it will resolve it's dependencies automatically and try to start the program.
P.S. But, regarding that it's an older code and it's unclear with what golang version(and packages versions) it was written it's likely that it will not work or, in some way, will be broken.
In my case, imports worked for a long time, then suddenly didn't.
I fixed it by running go mod tidy in the console.
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.
I want to run a go file, main package imported a local package which imported a github package. and get an error (missing .a file)
ENV:
$GOROOT=/usr/local/go
$GOPATH=/gopath
go version 1.6.3 (same problem in 1.6.2)
I tried to run a go file like this:
/gopath/src/myproj/main/app.go
package main
import (
"../http/server"
)
func main() {
server.Run()
}
/gopath/src/myproj/http/server/route.go
package server
import (
"github.com/gorilla/mux"
"net/http"
)
func Run(){
router := mux.NewRouter()
router.HandleFunc("/", handler)
http.Handle("/", router)
http.ListenAndServe("9090", nil)
}
func handler(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Hello"))
}
then,I run go get github.com/gorilla/mux and I can see there are files
/gopath/pkg/linux_amd64/github.com/gorilla/mux (there are context.a and mux.a)
/gopath/src/github.com/gorilla/mux
/gopath/src/github.com/gorilla/context
Yes, src file and pkg file(.a) has downloaded form github,
BUT , I run "go run main/app.go" , get
# command-line-arguments
/usr/local/go/pkg/tool/linux_amd64/link: cannot open file /usr/local/go/pkg/linux_amd64/github.com/gorilla/mux.a: open /usr/local/go/pkg/linux_amd64/github.com/gorilla/mux.a: no such file or directory
compiler does not find file in GOPATH, but GOROOT
if I copy $GOPATH/pkg files to $GOROOT/pkg was good.
And, if I import github.com/gorilla/mux in main package directly was fine too.
As JimB said don't use relative paths for imports
if you change "../http/server" to myproj/http/server it shouldn't have the linking problems anymore