golang中的“无法解决参考错误”

时间:2020-01-10 11:55:19

标签: go compiler-errors microservices boltdb

我目前正在使用BoltDB开发Go Microservices。如何解决golang中的“未解决参考错误”?

我有以下Go代码:

// 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

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

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

func (bc *BoltClient) Seed() {    
    bc.initializeBucket()
    bc.seedAccounts()
}
相关问题