In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you an example analysis of the principle of Go implementation, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Foreword:
Simple principle code description
Core: blocks and blocks to achieve main: example operation
BlockMain execution directory:
Package main import "Chains/core" func main () {sc: = core.NewBlockChain () sc.SendData ("send 1 btc to jacky") sc.SendData ("send 1 eos to jack") sc.Print ()}
Block Block:
Package core import ("crypto/sha256"encoding/hex"time") type Block struct {Index int64 / / Block number Timestamp int64 / / Block timestamp PreBlockHash string / / Last block hash Hash string / / current block hash value Data string / / Block data} func calculateHash (b Block) string {blockData: = string (b.Index) + string (b.Timestamp) + b.PreBlockHash hashInBytes: = Sha256.Sum256 ([] byte (blockData)) return hex.EncodeToString (hashInBytes [:])} func GenerateNewBlock (preBlock Block Data string) Block {newBlock: = Block {} newBlock.Index = preBlock.Index + 1 newBlock.PreBlockHash = preBlock.Hash newBlock.Timestamp = time.Now (). Unix () newBlock.Hash = calculateHash (newBlock) return newBlock} func GenerateGenesisBlock () Block {preBlock: = Block {} preBlock.Index = 1 preBlock.Hash = "return GenerateNewBlock (preBlock," Genesies Block ")}
How to realize BlockChain Block chain
Package core import ("log"fmt") type BlockChain struct {Blocks [] * Block} func NewBlockChain () * BlockChain {genesisBlock: = GenerateGenesisBlock () blockchain: = BlockChain {} blockchain.ApendBlock (& genesisBlock) return & blockchain} func (bc * BlockChain) SendData (data string) {preBlock: = bc.Blocks [len (bc.Blocks)-1] newBlock: = GenerateNewBlock (* preBlock) Data) bc.ApendBlock (& newBlock)} func (bc * BlockChain) ApendBlock (newBlock * Block) {if len (bc.Blocks) = = 0 {bc.Blocks = append (bc.Blocks,newBlock) return} if isValid (* newBlock,* bc.BlocksLen (bc.Blocks)-1]) {bc.Blocks = append (bc.Blocks) NewBlock)} else {log.Fatal ("invalid block")}} func (bc * BlockChain) Print () {for _, block: = range bc.Blocks {fmt.Printf ("Index:%d\ n", block.Index) fmt.Printf ("Pre.Hash:%s\ n", block.PreBlockHash) fmt.Printf ("Curr.Hash:%s\ n" Block.Hash) fmt.Printf ("Data:%s\ n", block.Data) fmt.Printf ("TimeStamp:%d\ n", block.Timestamp)}} func isValid (newBlock Block OldBlack Block) bool {if newBlock.Index-1! = oldBlack.Index {return false} if newBlock.PreBlockHash! = oldBlack.Hash {return false} if calculateHash (newBlock)! = newBlock.Hash {return false} return true} the above is an example analysis of the principle of Go implementation Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.