-1

我目前正在使用 BoltDB 开发 Go 微服务。如何解决 golang 中的“未解决的引用错误”?

我有以下围棋代码:

// Start seeding accounts
//Funtion Seed Gives me error as the functions which i passed in Seed Functions are not defined even i defined.
func (bc *BoltClient) Seed() {

    initializeBucket()  // <-- this line gives error as it's undefined even i defined.
    seedAccounts()      // <-- this line gives error as it's undefined even i defined.
}

// Creates an "AccountBucket" in our BoltDB. It will overwrite any existing bucket of the same name.
func (bc *BoltClient) initializeBucket() {
    bc.boltDB.Update(func(tx *bolt.Tx) error {
        _, err := tx.CreateBucket([]byte("AccountBucket"))
        if err != nil {
            return fmt.Errorf("create bucket failed: %s", err)
        }
        return nil
    })
}

func (bc *BoltClient) seedAccounts() {

    total := 100
    for i := 0; i < total; i++ {

        //generating key larger than 10000
        key := strconv.Itoa(10000 + i)

        //Creating instance of account structure
        acc := model.Account{
            Id:key,
            Name:"Person_" + strconv.Itoa(i),
        }

        //Serializing structure of JSON
        jsonBytes, _ := json.Marshal(acc)

        bc.boltDB.Update(func(tx *bolt.Tx) error {
            b := tx.Bucket([]byte("AccountBucket"))
            err := b.Put([]byte(key), jsonBytes)
            return err
        })
    }
    fmt.Printf("Seeded %v fake accounts...\n", total)
}

当我执行代码时,我收到以下错误:

# _/home/dev46/dev46/code/go_projects/src/callistaenterprise_/goblog/accountservice/dbclient
dbclient/boltclient.go:32:2: undefined: initializeBucket
dbclient/boltclient.go:33:2: undefined: seedAccounts

我究竟做错了什么?

4

1 回答 1

1

在 Go 中,与 Java 或 C++ 不同,方法是使用显式方法接收器调用的,即使它与当前接收器相同:

func (bc *BoltClient) Seed() {    
    bc.initializeBucket()
    bc.seedAccounts()
}
于 2020-01-10T12:55:44.487 回答