douchen9855 2019-01-13 23:41
浏览 20
已采纳

如何确保按顺序解决所有写入事务功能? 另外,为什么else函数没有执行?

I'm trying to create a very simple Bolt database called "ledger.db" that includes one Bucket, called "Users", which contains Usernames as a Key and Balances as the value that allows users to transfer their balance to one another. I am using Bolter to view the database in the command line

There are two problems, both contained in this transfer function issue resides in the transfer function.

The First: Inside the transfer function is an if/else. If the condition is true, it executes as it should. If it's false, nothing happens. There's no syntax errors and the program runs as though nothing is wrong, it just doesn't execute the else statement.

The Second: Even if the condition is true, when it executes, it doesn't update BOTH the respective balance values in the database. It updates the balance of the receiver, but it doesn't do the same for the sender. The mathematical operations are completed and the values are marshaled into a JSON-compatible format.

The problem is that the sender balance is not updated in the database.

Everything from the second "Success!" fmt.Println() function onward is not processed

I've tried changing the "db.Update()" to "db.Batch()". I've tried changing the order of the Put() functions. I've tried messing with goroutines and defer, but I have no clue how to use those, as I am rather new to golang.

func (from *User) transfer(to User, amount int) error{
        var fbalance int = 0
        var tbalance int = 0

        db, err := bolt.Open("ledger.db", 0600, nil)
        if err != nil {
              log.Fatal(err)
        }
        defer db.Close()



        return db.Update(func(tx *bolt.Tx) error {
              uBuck := tx.Bucket([]byte("Users"))

              json.Unmarshal(uBuck.Get([]byte(from.username)), &fbalance)
              json.Unmarshal(uBuck.Get([]byte(to.username)), &tbalance)

              if (amount <= fbalance) {
                fbalance = fbalance - amount

                encoded, err := json.Marshal(fbalance)
                if err != nil {
                  return err
                }

                tbalance = tbalance + amount

                encoded2, err := json.Marshal(tbalance)
                if err != nil {
                  return err
                }

                fmt.Println("Success!")

                c := uBuck

                err = c.Put([]byte(to.username), encoded2)
                return err

                fmt.Println("Success!")

                err = c.Put([]byte(from.username), encoded)
                return err

                fmt.Println("Success!")

              } else {
                return fmt.Errorf("Not enough in balance!", amount)
              }



            return nil


        })




        return nil
}

func main() {
    /*
    db, err := bolt.Open("ledger.db", 0600, nil)
    if err != nil {
          log.Fatal(err)
        }
    defer db.Close()
    */

    var b User = User{"Big", "jig", 50000, 0}
    var t User = User{"Trig", "pig", 40000, 0}

    // These two functions add each User to the database, they aren't 
    // the problem
    b.createUser()
    t.createUser()

    /*
    db.View(func(tx *bolt.Tx) error {
      c := tx.Bucket([]byte("Users"))
      get := c.Get([]byte(b.username))
      fmt.Printf("The return value %v",get)

      return nil

    })
    */

    t.transfer(b, 40000)


}

I expect the database to show Big:90000 Trig:0 from the beginning values of Big:50000 Trig:40000

Instead, the program outputs Big:90000 Trig:40000

  • 写回答

1条回答 默认 最新

  • duanhuan1147 2019-01-14 01:00
    关注

    You return unconditionally:

    c := uBuck
    
    err = c.Put([]byte(to.username), encoded2)
    return err
    
    fmt.Println("Success!")
    
    err = c.Put([]byte(from.username), encoded)
    return err
    
    fmt.Println("Success!")
    

    You are not returning and checking errors.

    json.Unmarshal(uBuck.Get([]byte(from.username)), &fbalance)
    json.Unmarshal(uBuck.Get([]byte(to.username)), &tbalance)
    
    t.transfer(b, 40000)
    

    And so on.


    Debug your code statement by statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败