go - 无法在 Go 应用程序的另一个包中使用函数

标签 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 的经验非常少。我做错了什么?

最佳答案

假设我们有一个名为 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)

关于go - 无法在 Go 应用程序的另一个包中使用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57161404/

相关文章:

.net - 在 .NET 中通信顺序进程

syntax - 如何将自定义结构放入堆栈然后能够访问所有字段?

go - 将 godoc 与一些注释/过滤器一起使用

windows - 如何使用 golang 在 Windows 上获取正确的文件基名

angularjs - 如何使用 Go 从一个 HTTP 请求中解析文件和 JSON 数据?

function - 无法打印完整的字符串数组

mysql - 全局变量未传递到下一个包

go - 如何截断 Golang 模板中的字符串

json - 使用 golang 解析 JSON HTTP 响应

docker - 从一个 Dockerized Go 服务获取查询到其他 Dockerized Go 服务