linux进程kill后显示deduct,go 示例 · PHP/Python/前端/Linux 等等 学习笔记 · 看云

[TOC]

## 示例

### 披萨订购

main.go

```

package main

import (

"fmt"

"log"

)

type account struct {

name string

}

func newAccount(accountName string) *account {

return &account{

name: accountName,

}

}

func (a *account) checkAccount(accountName string) error {

if a.name != accountName {

return fmt.Errorf("Account Name is incorrect")

}

fmt.Println("Account Verified")

return nil

}

type securityCode struct {

code int

}

func newSecurityCode(code int) *securityCode {

return &securityCode{

code: code,

}

}

func (s *securityCode) checkCode(incomingCode int) error {

if s.code != incomingCode {

return fmt.Errorf("Security Code is incorrect")

}

fmt.Println("SecurityCode Verified")

return nil

}

type wallet struct {

balance int

}

func newWallet() *wallet {

return &wallet{

balance: 0,

}

}

func (w *wallet) creditBalance(amount int) {

w.balance += amount

fmt.Println("Wallet balance added successfully")

return

}

func (w *wallet) debitBalance(amount int) error {

if w.balance < amount {

return fmt.Errorf("Balance is not sufficient")

}

fmt.Println("Wallet balance is Sufficient")

w.balance = w.balance - amount

return nil

}

type ledger struct {

}

func (s *ledger) makeEntry(accountID, txnType string, amount int) {

fmt.Printf("Make ledger entry for accountId %s with txnType %s for amount %d\n", accountID, txnType, amount)

return

}

type notification struct {

}

func (n *notification) sendWalletCreditNotification() {

fmt.Println("Sending wallet credit notification")

}

func (n *notification) sendWalletDebitNotification() {

fmt.Println("Sending wallet debit notification")

}

type walletFacade struct {

account *account

wallet *wallet

securityCode *securityCode

notification *notification

ledger *ledger

}

func newWalletFacade(accountID string, code int) *walletFacade {

fmt.Println("Starting create account")

walletFacacde := &walletFacade{

account: newAccount(accountID),

securityCode: newSecurityCode(code),

wallet: newWallet(),

notification: &notification{},

ledger: &ledger{},

}

fmt.Println("Account created")

return walletFacacde

}

func (w *walletFacade) addMoneyToWallet(accountID string, securityCode int, amount int) error {

fmt.Println("Starting add money to wallet")

err := w.account.checkAccount(accountID)

if err != nil {

return err

}

err = w.securityCode.checkCode(securityCode)

if err != nil {

return err

}

w.wallet.creditBalance(amount)

w.notification.sendWalletCreditNotification()

w.ledger.makeEntry(accountID, "credit", amount)

return nil

}

func (w *walletFacade) deductMoneyFromWallet(accountID string, securityCode int, amount int) error {

fmt.Println("Starting debit money from wallet")

err := w.account.checkAccount(accountID)

if err != nil {

return err

}

err = w.securityCode.checkCode(securityCode)

if err != nil {

return err

}

err = w.wallet.debitBalance(amount)

if err != nil {

return err

}

w.notification.sendWalletDebitNotification()

w.ledger.makeEntry(accountID, "credit", amount)

return nil

}

func main() {

fmt.Println()

walletFacade := newWalletFacade("abc", 1234)

fmt.Println()

err := walletFacade.addMoneyToWallet("abc", 1234, 10)

if err != nil {

log.Fatalf("Error: %s\n", err.Error())

}

fmt.Println()

err = walletFacade.deductMoneyFromWallet("abc", 1234, 5)

if err != nil {

log.Fatalf("Error: %s\n", err.Error())

}

}

```

输出

```

Starting create account

Account created

Starting add money to wallet

Account Verified

SecurityCode Verified

Wallet balance added successfully

Sending wallet credit notification

Make ledger entry for accountId abc with txnType credit for amount 10

Starting debit money from wallet

Account Verified

SecurityCode Verified

Wallet balance is Sufficient

Sending wallet debit notification

Make ledger entry for accountId abc with txnType credit for amount 5

```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值