[英]Unable to use function in another package in Go app
我有一個取決於幾個包的 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)
Save()
函數附加到Database
結構,而不是在包級別
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.