I have a go app depending on a few packages. However when I try to build my app it says the functions in an imported package are undefined.
File in main package (batch.go) using the functions:
package main
import (
"reflect"
db "bitbucket.org/b***/go-db"
)
// NewBatch creates a new batch
func NewBatch(orderID, employeeID int64) *Batch {
return &Batch{
OrderID: orderID,
EmployeeID: employeeID,
Flag: true,
}
}
// InsertBatch inserts a batch and all the underlying order lines and additions
func InsertBatch(b *Batch) (*Batch, error) {
err := db.Save(b)
InsertOrderLines(b.ID, b.OrderLines)
return b, err
}
File in sonicdb (aliased as db) with the functions:
package sonicdb
import (
"database/sql"
"fmt"
"reflect"
"strings"
)
// Scalar holds a scalar value of a database query, i.e. a count on a table
type Scalar struct {
N int64 `db:"n"`
}
// Repository interface
type Repository interface {
Get(sql string) ([]interface{}, error)
GetAndBind(sql string, i interface{}) ([]interface{}, error)
View(from string, criteria []Clause, selectors ...string) ([]interface{}, error)
Find(i interface{}, id int64) (interface{}, error)
FindAll(i interface{}) ([]interface{}, error)
FindBy(i interface{}, criteria []Clause) ([]interface{}, error)
FindByOrdered(i interface{}, criteria []Clause, ordering map[string]string) ([]interface{}, error)
FindOneBy(i interface{}, criteria []Clause) (interface{}, error)
FindOneByOrdered(i interface{}, criteria []Clause, ordering map[string]string) (interface{}, error)
First(i interface{}, criteria []Clause) (interface{}, error)
Count(i interface{}, criteria []Clause) (int64, error)
Save(i interface{}) error
Update(i interface{}) error
UpdateBy(i interface{}, criteria []Clause) (int64, error)
UpdateFields(i interface{}, fields []*Field) error
DeleteBy(i interface{}, criteria []Clause) (int64, error)
OneToOne(rv reflect.Value, c interface{}, join string) error
OneToMany(parent reflect.Value, child interface{}, mappedBy string) error
Relations(parent reflect.Value) interface{}
MarshalObject(i interface{}) (interface{}, error)
}
// Save handles a save action for a model
func (db *Database) Save(i interface{}) error {
id, err := doInsert(db.DB, i)
if err != nil {
return err
}
rv := reflect.ValueOf(i)
if rv.Type().Kind() == reflect.Ptr {
rv = rv.Elem()
}
for j := 0; j < rv.NumField(); j++ {
field := rv.Field(j)
fieldType := rv.Type().Field(j)
if !field.CanAddr() {
return fmt.Errorf("field `%s` cannot be addressed", fieldType.Name)
}
if !field.CanSet() {
return fmt.Errorf("field `%s` cannot be set", fieldType.Name)
}
if index, ok := fieldType.Tag.Lookup("index"); ok && index == "pk" {
field.SetInt(id)
}
}
return nil
}
Errors:
./batch.go:20:9: undefined: sonicdb.Save
The bitbucket.org packages are imported with 'go get bitbucket.org/name' and are now located in '~/go/src/bitbucket.org/name', which should be correct.
The packages ran on the computer of the previous developer of this project but since I took over, it will not build. It is probably an easy fix but my experience with Go is very minimal. What am I doing wrong?
Let's consider that we have a package called demo
package demo
type DataBase struct {
A int
B int
}
// function example
func DemoFunc(x int) int {
y := x + x
return y
}
// method example
func (db *DataBase) DemoMethod(x int) int {
y:= db.A + db.B + x
return y
}
To access a function or a method from main code, we can do the following,
package main
import (
"fmt"
dm "github.com/kamolhasan/stackOverflow/demo"
)
func main() {
// Now if you want to use the function
// DemoFunc() from demo package
// You can use it like following
value := dm.DemoFunc(34)
fmt.Println(value)
// If you want to use DemoMethod()
// from demo package, you need to declare the object first
// then call the method like following
DB := dm.DataBase{
A: 5,
B: 10,
}
value = DB.DemoMethod(34)
fmt.Println(value)
}
All you need to solve your problem is, declare an object of *Database
type and use the object to call the method. Do something like bellow:
var obj db.Database
err := obj.Save(b)
The Save()
function is attached to a Database
struct, not at the package level