无法在Go应用中的其他软件包中使用功能

时间:2019-07-23 09:52:57

标签: go

我有一个go应​​用,具体取决于一些软件包。但是,当我尝试构建我的应用程序时,它说导入的包中的功能未定义。

使用以下功能将文件保存在主包(batch.go)中:

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
}

sonicdb中的文件(别名为db),具有以下功能:

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
}

错误:

./batch.go:20:9: undefined: sonicdb.Save

bitbucket.org软件包是使用“ go get bitbucket.org/name”导入的,现在位于“〜/ go / src / bitbucket.org / name”中,这应该是正确的。

这些程序包在该项目的前一个开发人员的计算机上运行,​​但是自从我接手以来,该程序包无法构建。这可能很容易解决,但是我对Go的经验很少。我在做什么错了?

2 个答案:

答案 0 :(得分:0)

让我们考虑一下我们有一个名为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
}

要从主代码访问函数或方法,我们可以执行以下操作,

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)
}

您需要解决的所有问题是,声明一个*Database类型的对象,然后使用该对象来调用该方法。做类似下面的事情:

var obj db.Database
err := obj.Save(b)

答案 1 :(得分:0)

Save()函数附加在Database结构上,而不是在程序包级别