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

Package issue. #11

Closed harisgen79 closed 5 years ago

harisgen79 commented 5 years ago

I followed your steps and generated object box model. I also made a simple main.go file which contains:

package main

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

func initObjectBox() *objectbox.ObjectBox { objectBox, err := 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" box.Put(task) // Update box.Remove(task) // Delete }

Pretty much the same code you provided in your tutorial. When i run: pi@raspberrypi:~/internal/model $ go run main.go I get: main.go:5:2: use of internal package github.com/objectbox/objectbox-go/examples/tasks/internal/model not allowed.

go version go1.12.4 linux/arm.

greenrobot commented 5 years ago

Better also copy the model files to your project, too. In Go, you may not access "internal" package of dependencies.

harisgen79 commented 5 years ago

I created a separate folder, if this is what you meant, called "project", in which I placed all of these files and now I'm getting a different error:

pi@raspberrypi:\~/project $ ls main.go objectbox-model.go objectbox-model.json task.go task.obx.go pi@raspberrypi:~/project $ go run main.go main.go:5:2: import "github.com/objectbox/objectbox-go/examples/tasks/internal/model" is a program, not an importable package

vaind commented 5 years ago

This is not really related to ObjectBox but rather how Go handles modules & imports.

If you've copied the sources one-to-one, you also have to update the import path to reflect your actual project, e.g. something like replacing "github.com/objectbox/objectbox-go/examples/tasks" with "github.com/harisgen79/project".

If you're not using "go modules" your "project" directory needs to be in the right location in GOPATH

harisgen79 commented 5 years ago

Problem solved, thank you very much. Sorry for bothering. :)

vaind commented 5 years ago

No problem, I'm glad you've solved it.