Error run code "BUILDING AN APPLICATION WITH GO AND SQLITE"

Hello everybody,
I don’t have much programming experience but now I’m trying to learn a programming language well, in the end I chose GO. After reading a few books and doing some little tests with the code, I followed a more complete tutorial to try to make a first complete program. Unfortunately I got stuck almost immediately because by running the first part of the program I get errors and by myself I could not solve them, I ask you for help (sorry for the bad English). This is the first part of the code:

main.go

                                                                 
package main

import (
        "log"
        "github.com/dixonwille/wmenu/v5"
)

func main() {

        menu := wmenu.NewMenu("What would you like to do?")

        menu.Action(func(opts []wmenu.Opt) error { handleFunc(opts); return nil })

        menu.Option("Add a new Person", 0, true, nil)
        menu.Option("Find a Person", 1, false, nil)
        menu.Option("Update a Person's information", 2, false, nil)
        menu.Option("Delete a person by ID", 3, false, nil)
        menuerr := menu.Run()

        if menuerr != nil {
                log.Fatal(menuerr)
        }
}

the error:

go run main.go
main.go:5:2: cannot find package "github.com/dixonwille/wmenu/v5" in any of:
	/home/linuxbrew/.linuxbrew/Cellar/go/1.19.2/libexec/src/github.com/dixonwille/wmenu/v5 (from $GOROOT)
	/home/max/go/src/github.com/dixonwille/wmenu/v5 (from $GOPATH)

Where am I wrong? The package has been installed.
Can you help me?

Hi @canuto,

What’s in your go.mod file?

Does a go mod tidy help`

Hi Christophberger,
this is the go.mod of the complete project.

module jeremymorgan.com/gosqlite

go 1.17

require (
	github.com/daviddengcn/go-colortext v0.0.0-20180409174941-186a3d44e920 // indirect
	github.com/dixonwille/wlog/v3 v3.0.1 // indirect
	github.com/dixonwille/wmenu/v5 v5.1.0 // indirect
	github.com/mattn/go-isatty v0.0.11 // indirect
	github.com/mattn/go-sqlite3 v1.14.8 // indirect
	golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
)

The tutorial with the project I’m following can be found here:
allhandsontech.com/programming/golang/how-to-use-sqlite-with-go

Thanks for answering me.

Thanks for sharing the go.mod file. It looks fine except that I would expect wmenu to be a direct dependency.

To test this, I took the first two code snippets from the SQLite tutorial you mentioned (these are just enough so that the code compiles and runs), put them into a main.go file and created a go.mod file with go mod init.

Then I ran go mod tidy:

> go mod tidy                                            0s
go: downloading github.com/dixonwille/wmenu/v5 v5.1.0
go: downloading github.com/dixonwille/wlog/v3 v3.0.1
go: downloading github.com/mattn/go-isatty v0.0.11
go: downloading github.com/daviddengcn/go-colortext v0.0.0-20180409174941-186a3d44e920
go: downloading golang.org/x/sys v0.0.0-20191026070338-33540a1f6037
go: downloading github.com/stretchr/testify v1.4.0
go: downloading github.com/golangplus/testing v0.0.0-20180327235837-af21d9c3145e
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v2 v2.2.2
go: downloading github.com/davecgh/go-spew v1.1.0

go run main.go works fine:

> go run main.go                                        0s
1) *Add a new Person
2) Find a Person
3) Update a Person's information
4) Delete a person by ID
What would you like to do?
1
Adding a new Person

Questions:

  1. What is your output of go mod tidy?
  2. Does go clean -modcache && go mod tidy improve anything?
  3. If this does not help, what does go build return?

Thanks for the reply. Unfortunately I continue to have errors. You have to excuse me but this is my first time and so who knows how many mistakes I make. So I tried to do what you tried and more exactly:

I created main.go with this code:

package main

import (
        "log"
        "github.com/dixonwille/wmenu/v5"
)

func main() {

        menu := wmenu.NewMenu("What would you like to do?")

        menu.Action(func(opts []wmenu.Opt) error { handleFunc(opts); return nil })

        menu.Option("Add a new Person", 0, true, nil)
        menu.Option("Find a Person", 1, false, nil)
        menu.Option("Update a Person's information", 2, false, nil)
        menu.Option("Delete a person by ID", 3, false, nil)
        menuerr := menu.Run()

        if menuerr != nil {
                log.Fatal(menuerr)
        }
}

func handleFunc(opts []wmenu.Opt) {

        switch opts[0].Value {

        case 0:
                fmt.Println("Adding a new Person")
        case 1:
                fmt.Println("Finding a Person")
        case 2:
                fmt.Println("Update a Person's information")
        case 3:
                fmt.Println("Deleting a person by ID")
        case 4:
                fmt.Println("Quitting application")
        }
}

Then I tried to give the command “go mod init” but I get this error:

go: cannot determine module path for source directory /home/max/go/SqliteTest/Test1-Sqlite (outside GOPATH, module path must be specified)

So I created a go.mod file with this content:

module jeremymorgan.com/gosqlite

go 1.17

require github.com/dixonwille/wmenu/v5 v5.1.0

require (
        github.com/daviddengcn/go-colortext v0.0.0-20180409174941-186a3d44e920 // indirect
        github.com/dixonwille/wlog/v3 v3.0.1 // indirect
        github.com/mattn/go-isatty v0.0.11 // indirect
        golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
)

Running go mod tidy i get:

go: downloading github.com/stretchr/testify v1.4.0
go: downloading github.com/golangplus/testing v0.0.0-20180327235837-af21d9c3145e
go: downloading github.com/davecgh/go-spew v1.1.0
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v2 v2.2.2

go run main.go:

./main.go:30:3: undefined: fmt
./main.go:32:3: undefined: fmt
./main.go:34:3: undefined: fmt
./main.go:36:3: undefined: fmt
./main.go:38:3: undefined: fmt

Go build returns

# jeremymorgan.com/gosqlite
./main.go:30:3: undefined: fmt
./main.go:32:3: undefined: fmt
./main.go:34:3: undefined: fmt
./main.go:36:3: undefined: fmt
./main.go:38:3: undefined: fmt

You need to import “fmt”.

1 Like

This error occurs because go mod init needs the module path as an argument.

If you rename your current go.mod file and call

go mod init jeremymorgan.com/gosqlite

you should then get a go.mod file like the one you created manually.

As @mje already wrote, you need to import the fmt package, which is part of the standard library.

Tip: a Go-aware editor can automatically add or remove imports from the standard library as required, using the Go language server. (And also 3rd-party packages as long as the editor can resolve the package path.) So when you type, for example, fmt.Println, the editor inserts import "fmt" automatically.

OK thanks.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.