objectbox / objectbox-go

Embedded Go Database, the fast alternative to SQLite, gorm, etc.
https://objectbox.io
Apache License 2.0
1.07k stars 46 forks source link

Following GETTING STARTED guide does not work #28

Open FriesK1 opened 3 years ago

FriesK1 commented 3 years ago

:rotating_light: First, please check:

Description

When I add the code from the getting started guide into a main.go file and run "go run main.go", I receive the following error: package command-line-arguments main.go:5:2: use of internal package github.com/objectbox/objectbox-go/examples/tasks/internal/model not allowed

Basic info

Please complete the following information:

How to reproduce

Steps to reproduce the behavior:

  1. Enter code from user guide in files specified
  2. Run go generate
  3. Run go run
  4. Receive eror

Expected behavior

This should run the sample program documented

Code

`package main

import ( "fmt" "github.com/objectbox/objectbox-go/examples/tasks/internal/model" "github.com/objectbox/objectbox-go/objectbox" )

func initObjectBox() *objectbox.ObjectBox { objectBox, _ := objectbox.NewBuilder().Model(model.ObjectBoxModel()).Build() return objectBox }

func main() { // load objectbox ob := initObjectBox() defer ob.Close() // In a server app, you would just keep ob and close on shutdown

box := model.BoxForTask(ob)

// Create id, _ := box.Put(&model.Task{ Text: "Buy milk", })

task, _ := box.Get(id) // Read task.Text += " & some bread"

fmt.Printf("My Test Record:\n") fmt.Printf("%+v", task) fmt.Printf("\n\n")

box.Put(task) // Update count, _ := box.Count() fmt.Printf("After the add, there are %d tasks in the list\n", count)

box.Remove(task) // Delete count, _ = box.Count() fmt.Printf("After the remove, there are %d tasks in the list\n", count) }`

vaind commented 3 years ago

I guess the code block in this section is the culprit, right?. We should think about how to change the imports in the docs so that it's clear user needs to actually change them to their module path...

FriesK1 commented 3 years ago

I guess the code block in this section is the culprit, right?. We should think about how to change the imports in the docs so that it's clear user needs to actually change them to their module path...

I am not sure if this is a documentation only issue or not. I was able to work around the issue by using direct paths, but this is considered extremely bad form. The code we need to include should be generated into a vendor folder and documentation should be adjusted to identify the proper path from there, and vendoring needs to be turned on during installation, creating a go dependency issue.

This is probably not simply a documentation issue if done correctly due to changes in the language.

vaind commented 3 years ago

... The code we need to include...

That's exactly the misunderstanding I think needs addressing in the docs. You don't need to include any code from "github.com/objectbox/objectbox-go/examples/tasks/internal/model" - the whole "example/tasks" folder is an example of a self-contained application. You can copy-paste it, initialize it as your own go module (picking your own name, for example, github.com/FriesK1/objectbox-playground) and replace all the imports with the module name you've chosen (e.g. github.com/FriesK1/objectbox-playground).

sgbell commented 2 years ago

Just to throw in my 2 cents on this.. I couldn't get the command: go generate ./... working until I tried to run the command:

go test github.com/objectbox/objectbox-go

Running that command, then informed me of the commands I need to run to get objectbox-go working:

go get github.com/objectbox/objectbox-go go get github.com/objectbox/objectbox-go/cmd/objectbox-gogen@v1.6.1 go get github.com/objectbox/objectbox-go/examples/tasks/internal/model@v1.6.1

After installing those packages

go generate ./...

worked, and I was able to work with the package.

I'm new to golang, and it took me a few hours of messing around, to finally give the go test command a try, as I assumed I could just jump straight in.