gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:46:21 24 4
gpt4 key购买 nike

我有一个依赖于几个包的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com